views:

79

answers:

1

Sometimes I am getting a time out exception when reading an XML via a URL. Is there something I can do to prevent this, or is this a problem with the remote server? Below is my simple code:

XmlDocument doc = new XmlDocument();
doc.Load(XmlReader.Create(this.RssUrl));

I cam across a post that says KeepAlive may help, but I do not know how to pass that setting into the code above (or if that is even the correct solution): http://www.velocityreviews.com/forums/t95843-system-webexception-the-operation-has-timed-out.html

This is the exception, please help!

Exception Details: System.Net.WebException: The operation has timed out
Stack Trace: 

[WebException: The operation has timed out]
   System.Net.HttpWebRequest.GetResponse() +5375213
   System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials) +69
   System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) +3929371
   System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) +54
   System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext) +144
   System.Xml.XmlReader.Create(String inputUri) +8
+1  A: 

I had this problem earlier and found this unanswered question. An option I have found is to prevent the request from timing out at all, which you can do creating your own WebRequest with a timeout of Timeout.Infinite and passing it into the XmlReader.

WebRequest request = WebRequest.Create(this.RssUrl);
request.Timeout = Timeout.Infinite;

using (WebResponse response = request.GetResponse())
using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
{
    // Blah blah...
}

You can also set the KeepAlive if you use a HttpWebRequest, although it is actually true by default, so it should not make any difference.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.RssUrl);
request.KeepAlive = true;
Fara