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

时间:2015-04-27 02:28:37

标签: c# windows-server-2003 system.net.webexception

我在一台运行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;

7 个答案:

答案 0 :(得分:24)

将HttpWebRequest.KeepAlive设置为false对我不起作用。

由于我访问的是HTTPS页面,因此我必须将服务点安全协议设置为Tls12。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

请注意,还有其他SecurityProtocolTypes:

SecurityProtocolType.Ssl3 
SecurityProtocolType.Tls
SecurityProtocolType.Tls11

因此,如果Tls12不适合您,请尝试其余三个选项。

另请注意,您可以设置多个协议。在大多数情况下,这是优选的。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12| SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

答案 1 :(得分:9)

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

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

在这种情况下,可能会出现此问题,您将要下载一个作为过滤器链接的链接,而您无权下载该链接。

答案 4 :(得分:0)

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

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

答案 5 :(得分:0)

当我从带有API-KEY的命令行手动部署nux包到nexus服务器时,我遇到了这个错误。

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

enter image description here

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

答案 6 :(得分:0)

我在尝试使用HttpWebRequest下载rss文件时遇到此错误。当我在浏览器中测试并检查响应代码时,该URL很好。 在尝试了所有内容之后,我发现该网站可能由于用户代理而被阻止。

更改请求中的用户代理字符串有效:

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浏览器中键入“我的用户代理是什么”