我只在一台运行Windows Server 2003的服务器上收到此错误:
System.Net.WebException:基础连接已关闭:发送时发生意外错误。
这是我的代码......有什么想法吗?
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https:// URL HERE ");
//request.Headers.Add("Accept", "application/xml");
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(xml);
request.KeepAlive = false;
request.Accept = "application/xml";
request.ContentType = "application/xml; charset='UTF-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.Timeout = 10000;
request.ServicePoint.Expect100Continue = false;
当客户端计算机无法发送HTTP请求时,会发生此问题。客户端计算机无法发送HTTP请求,因为连接已关闭或不可用。客户端计算机发送大量数据时可能会发生此问题。要解决此问题,请参阅分辨率A,D,E,F和O.
将HttpWebRequest.KeepAlive设置为false对我不起作用。
由于我访问的是HTTPS页面,因此我必须将服务点安全协议设置为Tls12。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
请注意,还有其他SecurityProtocolType:
SecurityProtocolType.Ssl3
SecurityProtocolType.Tls
SecurityProtocolType.Tls11
因此,如果Tls12不适合您,请尝试其余三个选项。
另请注意,您可以设置多个协议。在大多数情况下这是优选的。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12| SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
使用.NET 4.5的RestSharp我得到了同样的错误。我用cURL测试了相同的URL,它工作正常。经过长时间的调试后,我发现设置SecurityProtocol解决了这个问题。
见:"The underlying connection was closed: An unexpected error occurred on a send." With SSL Certificate
在我的情况下,当我在环境之间交换URL时,我忘记从“https”中删除“s”。我在事故中用https击中了Localhost。如果您正在访问没有https证书或过期证书的http站点,则会发生同样的情况。
在这种情况下可能会出现此问题,您将需要下载作为筛选器链接的链接,并且您无权下载该链接。
尝试使用HttpWebRequest下载rss文件时出现此错误。当我在浏览器中测试并检查响应代码时,网址很好。在这里尝试了所有内容之后,我发现该网站可能是基于用户代理阻止的。
更改请求中的用户代理字符串:
let request = WebRequest.Create(url) :?> HttpWebRequest
request.UserAgent <- @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
let response = request.GetResponse()
此用户代理字符串来自在Google Chrome中键入“我的用户代理是什么”