2013-08-02 42 views
0

我有一個文本文件中的URL列表,我想測試它們是否都可以訪問。我在Windows PowerShell中觸發了以下命令,但在顯示前兩個請求的狀態之後,該命令在某處進行了掃描並且從不返回。我錯過了什麼嗎?Powershell命令:HttpWebRequest在獲取兩個請求後發現問題

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.  

回答

1

使用調用-的WebRequest代替:

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

foreach ($site in $sites) { 

    Invoke-WebRequest $site 
    $site 

} 
+0

與上面相同的行爲。 'cat。\ Test.txt | %{iwr「$ _」}'。 –

+0

適合我 - 所以你在PowerShell之外有一些問題 – Jimbo

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