prevent或处理超时与XmlReader.Create(URI) [英] Prevent or handle time out with XmlReader.Create(uri)

查看:182
本文介绍了prevent或处理超时与XmlReader.Create(URI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我通过一个URL读取XML时得到一个超时异常。有什么我可以做,以prevent这一点,或者这是与远程服务器有问题?下面是我简单的code:

  XmlDocument的文档=新的XmlDocument();
doc.Load(XmlReader.Create(this.RssUrl));
 

我凸轮跨越后,说的KeepAlive可能会有帮助,但我不知道怎么打发,设置到code以上(或者,甚至是正确的解决方案):<一href="http://www.velocityreviews.com/forums/t95843-system-webexception-the-operation-has-timed-out.html" rel="nofollow">http://www.velocityreviews.com/forums/t95843-system-webexception-the-operation-has-timed-out.html

这是个例外,请大家帮忙!

 异常详细信息:System.Net.WebException:操作已超时
堆栈跟踪:

[WebException:操作已超时]
   System.Net.HttpWebRequest.GetResponse()5375213
   System.Xml.XmlDownloadManager.GetNonFileStream(URI URI,ICredentials凭证)+69
   System.Xml.XmlDownloadManager.GetStream(URI URI,ICredentials凭证)3929371
   System.Xml.XmlUrlResolver.GetEntity(URI绝对URI,字符串的作用,类型ofObjectToReturn)+54
   System.Xml.XmlReader.Create(字符串inputUri,XmlReaderSettings设置,XmlParserContext inputContext)+144
   System.Xml.XmlReader.Create(字符串inputUri)+8
 

解决方案

我有这个问题前面,发现这个悬而未决的问题。我发现了一个选择是prevent超时可言,你可以做创建您自己的的WebRequest 与<一个超时href="http://msdn.microsoft.com/en-us/library/system.threading.timeout.infinite.aspx">Timeout.Infinite并将其传递到的XmlReader。

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

使用(WebResponse类响应= request.GetResponse())
使用(XmlReader的读者= XmlReader.Create(response.GetResponseStream()))
{
    //等等等等...
}
 

您还可以设置保持活动时间,如果你使用的HttpWebRequest,虽然它在默认情况下实际上是真的,所以应该不会有什么区别。

  HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(this.RssUrl);
request.KeepAlive = TRUE;
 

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

解决方案

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;

这篇关于prevent或处理超时与XmlReader.Create(URI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆