PowerShell ISE WebRequest在多次调用后失败

时间:2016-11-23 06:24:34

标签: powershell web httpwebrequest webrequest powershell-ise

如果从控制台运行,以下代码本身可以工作,但在使用PowerShell ISE运行时,它会在两次运行后失败并显示Exception calling "GetResponse" with "0" argument(s): "The operation has timed out"。必须重新启动ISE才能再次运行它。

$req = [System.Net.WebRequest]::CreateHttp("http://stackoverflow.com/")
$req.Timeout = 500
$req.GetResponse()

这是什么限制,是否有办法消除限制?

1 个答案:

答案 0 :(得分:0)

您似乎必须处理响应:

$req = [System.Net.WebRequest]::CreateHttp("http://stackoverflow.de/")
$req.Timeout = 5000
$response = $req.GetResponse()

$response.Dispose()

但是,您还可以使用内置 cmdlet Invoke-WebRequest

Invoke-WebRequest http://stackoverflow.de/
相关问题