Powershell命令:在获取两个请求后,HttpWebRequest卡住了

时间:2013-08-02 17:42:14

标签: powershell

我在文本文件中有一个URL列表,我想测试它们是否都可以访问。我在windows powershell中触发了以下命令,但在显示前两个请求的状态后,命令卡在某处并且永远不会返回。我错过了什么吗?

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.    

2 个答案:

答案 0 :(得分:1)

改为使用Invoke-WebRequest:

$sites = 'http://www.google.com','http://www.yahoo.com','http://www.bing.com'

foreach ($site in $sites) {

  Invoke-WebRequest $site
  $site

}

答案 1 :(得分:1)

从内存:您必须显式关闭响应流:

$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() }
}