gpt4 book ai didi

c# - HttpWebRequest - 出现 "Underlying connection was closed"错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:32:23 24 4
gpt4 key购买 nike

我与这个问题斗争了 2 天,我真的受够了。我发现没有任何效果。

检查这个简单的示例代码:

HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(updateInfoFileUrl);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //768 for TLS 1.1 and 3072 for TLS 1.2
ServicePointManager.ServerCertificateValidationCallback = delegate
{
return true;
};
fileReq.KeepAlive = false;

WebProxy proxy = new WebProxy();
proxy.BypassProxyOnLocal = false;
proxy.UseDefaultCredentials = true;
proxy.Credentials = CredentialCache.DefaultCredentials;
fileReq.Proxy = proxy;

HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();
Stream stream = fileResp.GetResponseStream();

我尝试了 SecurityProtocols 的不同组合。只使用一个,使用所有这些......没有任何效果。我尝试了 HttpWebRequest、HttpClient 甚至 XmlDocument.Load。什么都不管用。我每次都遇到错误。

它有什么问题?

这是我想阅读的文档: https://www.example.com/info.xml

浏览器显示它。甚至 PostMan 也会显示它。

最佳答案

做了一些调试。这不是您的代码的问题,但服务器需要用户代理设置。作品:

HttpWebRequest fileReq = (HttpWebRequest)WebRequest.Create(@"https://www.example.com/info.xml");
fileReq.UserAgent = "sadsad";
fileReq.Method = "GET";

HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();
Stream stream = fileResp.GetResponseStream();

失败:

HttpWebRequest fileReq = (HttpWebRequest)WebRequest.Create(@"https://www.example.com/info.xml");

// fileReq.UserAgent = "sadsad";
fileReq.Method = "GET";

HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();
Stream stream = fileResp.GetResponseStream();

关于c# - HttpWebRequest - 出现 "Underlying connection was closed"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56838503/

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