Powershell - 增加从URL检索XML的超时时间

aph*_*ria 9 powershell

我正在尝试从URL检索XML流.对于大多数网址,我的代码工作正常.但是,我有几个URL超时.相关网址从Internet Explorer工作.

$webclient=New-Object "System.Net.WebClient"
[xml]$data=$webclient.DownloadString($url)
Run Code Online (Sandbox Code Playgroud)

所以,我去寻找一种方法来增加超时时间.从我读过的内容来看,我相信我不能这样做System.Net.WebClient.我想我需要使用System.Net.WebRequest,但我不能让它工作.我一直在研究的代码如下:

$myHttpWebRequest = [system.net.WebRequest]::Create($url)
$myHttpWebRequest.Timeout = 600000
$myHttpWebResponse = $myHttpWebRequest.GetResponse()
$sr = New-Object System.IO.StreamReader($response.GetResponseStream())
[xml]$xml = [xml]$sr.ReadToEnd()
Run Code Online (Sandbox Code Playgroud)

我正在尝试访问的网址是我公司的内部网址,因此我无法发布这些网址.但是,它们确实在IE中工作,实际的URL应该是无关紧要的.

想法?

编辑:初步测试显示添加$myHttpWebRequest.AuthenticationLevel = "None"作品.谢谢Scott Saad.

Sco*_*aad 6

默认情况下,WebRequest.AuthenticationLevel设置为MutualAuthRequested,因此它将等待某种类型的身份验证响应.因此,在等待身份验证发生时可能会超出超时.看起来你并没有弄乱凭证,所以除非你需要身份验证,否则你可能不会需要这个.创建WebRequest后尝试以下内容:

$myHttpWebRequest.AuthenticationLevel = "None"
Run Code Online (Sandbox Code Playgroud)

我希望这有助于解决问题.