Prevent or handle time out with XmlReader.Create(uri)

前端 未结 1 1025
说谎
说谎 2021-01-13 17:29

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

相关标签:
1条回答
  • 2021-01-13 18:05

    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;
    
    0 讨论(0)
提交回复
热议问题