gpt4 book ai didi

c# - 无法从传输连接读取数据 : An existing connection was forcibly closed by the remote host in C#

转载 作者:太空狗 更新时间:2023-10-30 00:31:55 25 4
gpt4 key购买 nike

我有一个代码,我在其中发送 URL 请求并接收响应并将其存储为字符串 as

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 在博客文章中概述了以下发现:An existing connection was forcibly closed by the remote host

上述帖子中提到的步骤包括:

WebRequest.KeepAlive 设置为 False

WebRequest.ProtocolVersion 设置为 HttpVersion.Version10

WebRequest.ServicePoint.ConnectionLimit 设置为 1

它确实对我有用,但我还没有在多台主机上测试过它。但是,我强烈建议您阅读这篇博文,因为它更详细。

如果链接断开,这里是 Archive.org cached version .

关于c# - 无法从传输连接读取数据 : An existing connection was forcibly closed by the remote host in C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21446489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com