C#System.Net.WebException:底层连接已关闭:发送时发生意外错误

问题描述 投票:13回答:7

我只在一台运行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;
c# windows-server-2003 system.net.webexception
7个回答
0
投票

当客户端计算机无法发送HTTP请求时,会发生此问题。客户端计算机无法发送HTTP请求,因为连接已关闭或不可用。客户端计算机发送大量数据时可能会发生此问题。要解决此问题,请参阅分辨率A,D,E,F和O.

https://support.microsoft.com/en-us/kb/915599


24
投票

将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;

9
投票

使用.NET 4.5的RestSharp我得到了同样的错误。我用cURL测试了相同的URL,它工作正常。经过长时间的调试后,我发现设置SecurityProtocol解决了这个问题。

见:"The underlying connection was closed: An unexpected error occurred on a send." With SSL Certificate


1
投票

在我的情况下,当我在环境之间交换URL时,我忘记从“https”中删除“s”。我在事故中用https击中了Localhost。如果您正在访问没有https证书或过期证书的http站点,则会发生同样的情况。


1
投票

在这种情况下可能会出现此问题,您将需要下载作为筛选器链接的链接,并且您无权下载该链接。


0
投票

我在使用API​​-KEY从命令行手动部署nuget包到nexus服务器时遇到了这个错误。

我已经检查了nexus服务器配置,并且我已经意识到Nexus NuGet API-Key Realm未激活。我已经激活它并再次尝试,每一件事都很好。

enter image description here

因此,您应该检查服务器端以确认您已激活相关领域。


0
投票

尝试使用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中键入“我的用户代理是什么”

© www.soinside.com 2019 - 2024. All rights reserved.