Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - Increase the timeout for retrieving XML from a URL

Tags:

powershell

I'm trying to retrieve an XML stream from a URL. For most URLs my code below works fine. But, I have a couple URLs that timeout. The URLs in question do work from Internet Explorer.

$webclient=New-Object "System.Net.WebClient"
[xml]$data=$webclient.DownloadString($url)

So, I went searching for a way to increase the timeout period. From what I've read, I believe I cannot do this using System.Net.WebClient. I think I need to use System.Net.WebRequest instead, but I cannot get it to work. The code I've been working on is below:

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

The URLs I'm trying to access are internal to my company, so I can't post them. But, they do work in IE and the actual URL should be irrelevant.

Ideas?

EDIT: Preliminary testing shows that adding $myHttpWebRequest.AuthenticationLevel = "None" works. Thanks Scott Saad.

like image 538
aphoria Avatar asked Oct 16 '08 17:10

aphoria


1 Answers

By default the WebRequest.AuthenticationLevel is set to MutualAuthRequested, therefore it will wait for some type of authentication response. Therefore, a timeout is probably being exceeded while waiting for the authentication to occur. It didn't look like you were messing with the Credentials so unless you require authentication, you probably won't need this. Try something like the following after you create your WebRequest:

$myHttpWebRequest.AuthenticationLevel = "None"

I hope this helps solve the problem.

like image 78
Scott Saad Avatar answered Oct 14 '22 10:10

Scott Saad