C#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; 

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

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

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

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

请参阅: “基础连接已关闭:发送时发生意外错误。” 使用SSL证书

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

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

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

在此处输入图像描述

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

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