Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell:为什么System.Net.WebClient在同一主机的第三次openRead上失败_Powershell_Webclient_Powershell 4.0 - Fatal编程技术网

Powershell:为什么System.Net.WebClient在同一主机的第三次openRead上失败

Powershell:为什么System.Net.WebClient在同一主机的第三次openRead上失败,powershell,webclient,powershell-4.0,Powershell,Webclient,Powershell 4.0,我已经创建了一个Powershell脚本来检查一堆URL。下面是使用WebClient逐个读取url列表的代码片段 try { $log.debugFormat("Now checking endpoint={0}", $checkUrl) $wc.OpenRead($checkUrl) $log.infoFormat("guid={0} loop={1} endpoint={2} status=success", ($guid, $i, $checkUrl)) } ca

我已经创建了一个Powershell脚本来检查一堆URL。下面是使用WebClient逐个读取url列表的代码片段

try {
    $log.debugFormat("Now checking endpoint={0}", $checkUrl)
    $wc.OpenRead($checkUrl)
    $log.infoFormat("guid={0} loop={1} endpoint={2} status=success", ($guid, $i, $checkUrl))
} catch [System.Net.WebException] {
    $log.errorFormat("guid={0} Could not connect to {1}", ($guid, $checkUrl))
    $ErrorMessage = $_.Exception.Message
    $log.errorFormat("{0}", $ErrorMessage)
奇怪的是,对于我提供的每一台主机,它每三次尝试就会超时。首先,我花了数小时对Web服务器配置进行故障排除,现在才意识到,当第三次尝试连接到同一主机时,是Webclient本身以某种方式超时

我已尝试添加
$wc.Dispose()
,并尝试每次在循环中创建对象
$wc=New object System.Net.WebClient
,但没有帮助


版本为Windows 2012上的Powershell-命令“Write Host$psversiontable.psversion”4.0。

您需要关闭流,例如:

$stream = $wc.OpenRead($checkUrl)
$stream.Close()

处置
WebClient
也是个好主意

您需要关闭流,例如:

$stream = $wc.OpenRead($checkUrl)
$stream.Close()

处置
WebClient
也是个好主意

Magic,在
finally
-块中添加了
$stream.close()
$wc.dispose()
,解决了问题。Magic在
finally
-块中添加了
$stream.close()
$wc.dispose()
,解决了问题。