如何在 Powershell 中转义 Invoke-Webrequest
How to escape an Invoke-Webrequest in Powershell
我有一个网络设备,其状态页面可通过 Java 小程序访问。使用 Fiddler 我能够找到状态的 http 提要,但页面不断刷新。 (Firefox 显示页面但不断刷新,Chrome 将其视为无扩展名文件并尝试保存它但从未完成,因为总是有更多数据。)
状态页面使用 NTLM 身份验证,所以我虽然会使用 Invoke-Webrequest。以下代码登录并开始下载页面,但由于它的恒定数据流永远不会结束:
$url = "http://xxx.xxx.xxx.xxx/api/notify"
$user = "user"
$pass= "password"
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($user, $secpasswd)
$data = Invoke-restmethod $url -Credential $credential
有没有办法在接收到一定数量的字符后从 Invoke-Webrequest 中逃脱?或者有更好的方法吗?
我认为 Invoke-WebRequest
不可行,但您可以改用 .NET WebRequest
和 StreamReader
类。示例:
$sUri = "http://www.w3.org" # Replace with your URI
$sEncoding = "utf-8"
$iCharactersToRead = 1000 # How many characters we want to read
$sAuthType = "NTLM"
$sUserName = "username"
$sPassword = "password"
# Creating a new HttpWebRequest object.
[System.Net.HttpWebRequest]$oHttpWebRequest = [System.Net.WebRequest]::Create($sUri)
# This may be superflous if your HTTP server doesn't use compression.
$oHttpWebRequest.AutomaticDecompression = `
([System.Net.DecompressionMethods]::Deflate `
-bor [System.Net.DecompressionMethods]::GZip)
# Since you have NTLM auth, you need to add a credential.
$oCredential = New-Object -TypeName System.Net.NetworkCredential($sUserName, $sPassword)
$oCredentialCache = New-Object -TypeName System.Net.CredentialCache
$oCredentialCache.Add($sUri, "NTLM", $oCredential)
$oHttpWebRequest.Credentials = $oCredentialCache
# Creating a StreamReader object that will read the response stream.
$oResponseStream = $oHttpWebRequest.GetResponse().GetResponseStream()
$oContentReader = New-Object -TypeName System.IO.StreamReader($oResponseStream,
[System.Text.Encoding]::GetEncoding($sEncoding), $true)
# Trying to read the specified number of characters from the stream.
$sContent = [String]::Empty
try {
for ($i = 0; $i -lt $iCharactersToRead; $i++) {
$sContent += [Char]$oContentReader.Read()
}
}
finally {
$oContentReader.Close()
}
您可能需要指定另一个编码名称,而不是 utf-8
,具体取决于您的 HTTP 服务器使用的编码。有关详细信息,请参阅 Encoding.WebName 参考资料。
我有一个网络设备,其状态页面可通过 Java 小程序访问。使用 Fiddler 我能够找到状态的 http 提要,但页面不断刷新。 (Firefox 显示页面但不断刷新,Chrome 将其视为无扩展名文件并尝试保存它但从未完成,因为总是有更多数据。)
状态页面使用 NTLM 身份验证,所以我虽然会使用 Invoke-Webrequest。以下代码登录并开始下载页面,但由于它的恒定数据流永远不会结束:
$url = "http://xxx.xxx.xxx.xxx/api/notify"
$user = "user"
$pass= "password"
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($user, $secpasswd)
$data = Invoke-restmethod $url -Credential $credential
有没有办法在接收到一定数量的字符后从 Invoke-Webrequest 中逃脱?或者有更好的方法吗?
我认为 Invoke-WebRequest
不可行,但您可以改用 .NET WebRequest
和 StreamReader
类。示例:
$sUri = "http://www.w3.org" # Replace with your URI
$sEncoding = "utf-8"
$iCharactersToRead = 1000 # How many characters we want to read
$sAuthType = "NTLM"
$sUserName = "username"
$sPassword = "password"
# Creating a new HttpWebRequest object.
[System.Net.HttpWebRequest]$oHttpWebRequest = [System.Net.WebRequest]::Create($sUri)
# This may be superflous if your HTTP server doesn't use compression.
$oHttpWebRequest.AutomaticDecompression = `
([System.Net.DecompressionMethods]::Deflate `
-bor [System.Net.DecompressionMethods]::GZip)
# Since you have NTLM auth, you need to add a credential.
$oCredential = New-Object -TypeName System.Net.NetworkCredential($sUserName, $sPassword)
$oCredentialCache = New-Object -TypeName System.Net.CredentialCache
$oCredentialCache.Add($sUri, "NTLM", $oCredential)
$oHttpWebRequest.Credentials = $oCredentialCache
# Creating a StreamReader object that will read the response stream.
$oResponseStream = $oHttpWebRequest.GetResponse().GetResponseStream()
$oContentReader = New-Object -TypeName System.IO.StreamReader($oResponseStream,
[System.Text.Encoding]::GetEncoding($sEncoding), $true)
# Trying to read the specified number of characters from the stream.
$sContent = [String]::Empty
try {
for ($i = 0; $i -lt $iCharactersToRead; $i++) {
$sContent += [Char]$oContentReader.Read()
}
}
finally {
$oContentReader.Close()
}
您可能需要指定另一个编码名称,而不是 utf-8
,具体取决于您的 HTTP 服务器使用的编码。有关详细信息,请参阅 Encoding.WebName 参考资料。