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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29886223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:20:44  来源:igfitidea点击:

C# System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send

c#windows-server-2003system.net.webexception

提问by Jon

I'm getting this error on just one server running Windows Server 2003:

我仅在一台运行 Windows Server 2003 的服务器上收到此错误:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send.

System.Net.WebException:基础连接已关闭:发送时发生意外错误。



Here's my code... Any ideas?

这是我的代码......有什么想法吗?

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;

采纳答案by Dongming Yan

This problem occurs when the client computer cannot send an HTTP request. The client computer cannot send the HTTP request because the connection has been closed or is unavailable. This problem may occur when the client computer is sending lots of data. To resolve this problem, see resolutions A, D, E, F, and O.

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

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

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

回答by KevinVictor

I was getting the same error, using RestSharp with .NET 4.5. I tested the same URL with cURL and it worked fine. After a long time debugging I found that setting the SecurityProtocol fixed the issue.

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

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

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

回答by Suamere

In my case, I forgot to remove the "s" from "https" when I was swapping URLs between environments. I was hitting Localhost with https on accident. Same thing would occur if you are hitting an http site with no https certificate, or an expired certificate.

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

回答by Bartho Bernsmann

Setting the HttpWebRequest.KeepAlive to false didn't work for me.

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

Since I was accessing a HTTPS page I had to set the Service Point Security Protocol to Tls12.

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

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Notice that there are other SecurityProtocolTypes:

请注意,还有其他 SecurityProtocolTypes:

SecurityProtocolType.Ssl3 
SecurityProtocolType.Tls
SecurityProtocolType.Tls11

So if the Tls12 doesn't work for you, try the three remaining options.

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

Also notice you can set multiple protocols. This is preferable on most cases.

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

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

Edit: Since this is a choice of security standards it's obviously best to go with the latest (TLS 1.2 as of writing this), and not just doing what works. In fact, SSL3 has been officially prohibited from use since 2015and TLS 1.0 and TLS 1.1 will likely be prohibited soon as well. source: @aske-b

编辑:由于这是安全标准的选择,显然最好使用最新的(撰写本文时为 TLS 1.2),而不仅仅是做有效的工作。事实上,自 2015 年以来,SSL3 已被正式禁止使用TLS 1.0 和 TLS 1.1 也可能很快被禁止。来源:@aske-b

回答by wasabi

I have faced with this error while I was deploying a nuget package to nexus server manually from command line with API-KEY.

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

I have checked the nexus server configuration and I have realized Nexus NuGet API-Key Realm is not activated. I have activated it and tried again, everythings worked fine.

我检查了 nexus 服务器配置,发现 Nexus NuGet API-Key Realm 未激活。我已经激活它并再次尝试,一切正常。

enter image description here

在此处输入图片说明

So, You should check server side to confirm you have activated related realms.

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

回答by mohammad almasi

This problem may occur in this case, you will want to download a link that is a filter link and you do not have permission to download that link.

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

回答by chuckc

I was getting this error trying to download an rss file with HttpWebRequest. When I tested in a browser, and checked the response codes, the url was fine. After trying everything here, it occurred to me the site might be blocking based on User Agent.

我在尝试使用 HttpWebRequest 下载 rss 文件时遇到此错误。当我在浏览器中测试并检查响应代码时,url 很好。在这里尝试了一切之后,我发现该站点可能会基于用户代理进行阻止。

Changing the User Agent string in the request worked :

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

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()

This User Agent String came from typing "what's my user agent" in Google Chrome

这个用户代理字符串来自在谷歌浏览器中输入“我的用户代理是什么”