Powershell命令:HttpWebRequest在获取两个请求后停止
我在一个文本文件中有一个URL列表,我想测试它们是否都是可访问的。我在windows powershell中启动了以下命令,但不知何故,在显示了前两个请求的状态后,该命令在某个地方死机,永远不会返回。我错过什么了吗Powershell命令:HttpWebRequest在获取两个请求后停止,powershell,Powershell,我在一个文本文件中有一个URL列表,我想测试它们是否都是可访问的。我在windows powershell中启动了以下命令,但不知何故,在显示了前两个请求的状态后,该命令在某个地方死机,永远不会返回。我错过什么了吗 cat .\Test.txt | % { [system.Net.WebRequest]::Create("$_").GetResponse().StatusCode } 文本文件 http://www.google.com http://www.yahoo.com http://
cat .\Test.txt | % { [system.Net.WebRequest]::Create("$_").GetResponse().StatusCode }
文本文件
http://www.google.com
http://www.yahoo.com
http://www.bing.com
输出:
OK
OK
----> after that it stucks.
请改用Invoke WebRequest:
$sites = 'http://www.google.com','http://www.yahoo.com','http://www.bing.com'
foreach ($site in $sites) {
Invoke-WebRequest $site
$site
}
从内存:必须显式关闭响应流:
$req = [System.Net.HttpWebRequest]::Create($aRequestUrl);
$response = $null
try
{
$response = $req.GetResponse()
# do something with the response
}
finally
{
# Clear the response, otherwise the next HttpWebRequest may fail... (don't know why)
if ($response -ne $null) { $response.Close() }
}
与上述行为相同
cat.\Test.txt |%{iwr“$\u”}
适合我-所以在powershell之外您遇到了一些问题