Powershell:为什么System.Net.WebClient在同一主机的第三次openRead上失败
我已经创建了一个Powershell脚本来检查一堆URL。下面是使用WebClient逐个读取url列表的代码片段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
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()
,解决了问题。