我为这个问题奋斗了两天,我真的受够了。我找不到任何作品。

检查以下简单的示例代码:

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-收到“底层连接已关闭”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56838503/

10-13 07:07