我有一个代码,我在其中发送 URL 请求并接收响应并将其存储为字符串
public String GenerateXML(String q)// Here 'q' is the URL
{
// Generating the XML file for reference
// Getting the response in XML format from the URL
Debug.WriteLine("The Http URL after URL encoding :" + q);
try
{
Uri signs1 = new Uri(q);
//Debug.WriteLine("The Requested URL for getting the XML data :" + re);
WebRequest request1 = WebRequest.Create(signs1);
HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
//HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
Stream receiveStream = response1.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
String ab = readStream.ReadToEnd();// The mentioned error is showing up here.
// Debug.WriteLine("The data :"+a);
//XmlDocument content2 = new XmlDocument();
// content2.LoadXml(ab);
// content2.Save("C:/Users/Administrator/Downloads/direct.xml");
return ab;
}
catch (System.Net.WebException ex)
{
Debug.WriteLine("Exception caught :" + ex);
return null;
}
}
为什么连接被远程主机关闭?
消除错误或至少忽略错误并继续其他 URL 请求的可能性有哪些?我已经包含了 try 和 catch 以避免任何错误并继续运行而不停止。在互联网上搜索解决方案,但这个特定问题的解决方案非常具体。请提供任何帮助。
提前致谢。
我遇到了与不同主机强制关闭连接的类似问题。看来这个问题可以通过改变各种属性来解决WebRequest
目的。
briancaos 在博客文章中概述了以下发现:现有连接被远程主机强制关闭
上述帖子中提到的步骤包括:
Set WebRequest.KeepAlive
to False
.
Set WebRequest.ProtocolVersion
to HttpVersion.Version10
.
Set WebRequest.ServicePoint.ConnectionLimit
to 1
它确实对我有用,但我还没有在多个主机上测试过它。不过,我强烈建议您阅读这篇文章,因为它更详细。
如果链接损坏,这里是 Archive.org缓存版本.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)