XML文档System.Net.WebException [英] XML Document System.Net.WebException

查看:133
本文介绍了XML文档System.Net.WebException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这部分代码引发异常。如何解决这个问题?

  XmlDocument locrequest = new XmlDocument(); 
locrequest.Load(String.Format(http://api.geoips.com/ip/{0}/key/.myapikey./output/xml/,player.IP));
XmlNode root = locrequest.DocumentElement;
string from = root.SelectSingleNode(country_name)。InnerText.ToLower();




System.Net.WebException:错误:ConnectFailure(没有足够的缓冲区空间可用)


完整错误 http ://pastebin.com/8u6MpeWS



编辑:
如果我添加
locrequest.XmlResolver = null;
它引发另一个异常:


System.NullReferenceException:对象引用未设置为

解决方案

Ingo,



在此问题的基础上建立您的原始帖子这里,也许这将把你的作品放在一起更清楚一些。假设对于连接失败而言,有一些更基本的连接失败,而不管服务器是什么原因,不管怎样,都不接受连接,您可以通过使用Aghilas指出的XmlDocument类的默认Load()机制来绕过WebRequest对象。



WebRequest对象与您指定的URL联系,然后使用Request / Response模型创建Response对象。从该Response对象中,您可以通过GetResponseStream()方法获取响应数据的;这就是Aghilas展示的。然后,您可以将该流放在他的示例中的变量dataStream中,并将其传递给期望Stream的XmlDocument.Load()方法的重载:

  //假设Aghilas的先前代码在这里是适当的... 
流dataStream = response.GetResponseStream();
XmlDocument locRequest = new XmlDocument();
locRequest.Load(dataStream);

希望这有帮助。


This part of my code throws an exception. How can I fix that?

XmlDocument locrequest = new XmlDocument();
locrequest.Load(String.Format("http://api.geoips.com/ip/{0}/key/.myapikey./output/xml/", player.IP));
XmlNode root = locrequest.DocumentElement;
string from = root.SelectSingleNode("country_name").InnerText.ToLower();

System.Net.WebException: Error: ConnectFailure (Not enough buffer space is available)

Full error http://pastebin.com/8u6MpeWS

Edit: If I add locrequest.XmlResolver = null; it throws another exception:

System.NullReferenceException: Object reference not set to an instance of an object

What's wrong with that?

解决方案

Ingo,

Building on your original post for this problem here, perhaps this will put the pieces together for you a bit more clearly. Assuming that there is something more basic at hand for the connection failures than just that the server is, for whatever reason, not accepting connections, you can bypass the default Load() mechanism of the XmlDocument class by using, as Aghilas pointed out, a WebRequest object.

A WebRequest object contacts the URL you specify, and then uses the Request/Response model to create a Response object. From that Response object, you can get a stream to the data for the response via the GetResponseStream() method; that's what Aghilas demonstrated. You can then take that stream, placed in the variable dataStream in his sample, and pass that to the overload for the XmlDocument.Load() method that expects a Stream:

   //assuming prior code from Aghilas is here as appropriate...
   Stream dataStream = response.GetResponseStream();
   XmlDocument locRequest = new XmlDocument();
   locRequest.Load(dataStream);

Hope this helps.

这篇关于XML文档System.Net.WebException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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