C# System.Net.WebException:底层连接已关闭:连接意外关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1459475/
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-06 17:46:03  来源:igfitidea点击:

System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly

c#asp.net

提问by Kwah009

I'm writing a .NET application which is supposed to post data to another .NET application. I use the following code to request the login page

我正在编写一个 .NET 应用程序,它应该将数据发布到另一个 .NET 应用程序。我使用以下代码请求登录页面

WebProxy proxy = new WebProxy("http://proxy:80/", true);
HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
//proxy.Credentials = new NetworkCredential("myusername", "mypassword", "domain"); 
// webRequest.Proxy = proxy;
webRequest.Proxy = WebRequest.DefaultWebProxy;

StreamReader responseReader = new StreamReader
                                  (webRequest.GetResponse().GetResponseStream());
string responseData = responseReader.ReadToEnd();

but it fails on this line

但它在这条线上失败了

StreamReader responseReader = new StreamReader
                                  (webRequest.GetResponse().GetResponseStream());

with the error message :

带有错误消息:

System.Net.WebException: The underlying connection was closed: The connection was 
                         closed unexpectedly.

采纳答案by Rik

I encountered the same exception a while ago and I remember that this happens in some casesdue to a bug in .NET. You can work around this by setting the Timeout and ReadWriteTimeout of the request to higher values, or set KeepAlive to false.

不久前我遇到了同样的异常,我记得在某些情况下会发生这种情况由于 .NET 中的错误。您可以通过将请求的 Timeout 和 ReadWriteTimeout 设置为更高的值或将 KeepAlive 设置为 false 来解决此问题。

This would only be a workaround, though, so I suggest you try to find the actual root cause before assuming anything.

不过,这只是一种解决方法,因此我建议您在做出任何假设之前尝试找到实际的根本原因。

I'll try to come up with some web references, in the mean time, look at Big files uploading (WebException: The connection was closed unexpectedly)

我会尝试提出一些网络参考资料,同时查看 上传的大文件(WebException:连接意外关闭)

回答by Rik

If you are using .NET 2.0 or above can you enable network tracing and see what's actually happening over the wire. In that way you can get more information about this particular exception.

如果您使用 .NET 2.0 或更高版本,您可以启用网络跟踪并查看网络上实际发生的情况。通过这种方式,您可以获得有关此特定异常的更多信息。

See following link for more details, http://msdn.microsoft.com/en-us/library/hyb3xww8%28VS.80%29.aspx

有关更多详细信息,请参阅以下链接, http://msdn.microsoft.com/en-us/library/hyb3xww8%28VS.80%29.aspx

回答by Erik A. Brandstadmoen

Seems like to possible issues:

似乎可能的问题:

  1. You never assign the proxy you create to your HttpWebRequest

    WebProxy **proxy** = new WebProxy("http://proxy:80/", true);
    HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
    //proxy.Credentials = new NetworkCredential("myusername", "mypassword", "domain"); 
    // webRequest.Proxy = proxy;
    webRequest.Proxy = **WebRequest.DefaultWebProxy**;
    

    You should assign it like this:

    WebProxy proxy = new WebProxy("http://proxy:80/", true);
    HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
    webRequest.Proxy = proxy;
    

    (notice the difference in the last line).

  2. You use port 80 on your proxy. Sure that is correct? Many proxies use port 8080.

  1. 您永远不会将您创建的代理分配给您的 HttpWebRequest

    WebProxy **proxy** = new WebProxy("http://proxy:80/", true);
    HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
    //proxy.Credentials = new NetworkCredential("myusername", "mypassword", "domain"); 
    // webRequest.Proxy = proxy;
    webRequest.Proxy = **WebRequest.DefaultWebProxy**;
    

    你应该像这样分配它:

    WebProxy proxy = new WebProxy("http://proxy:80/", true);
    HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
    webRequest.Proxy = proxy;
    

    (注意最后一行的区别)。

  2. 您在代理上使用端口 80。确定这是正确的?许多代理使用端口 8080。

回答by Mak

myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;

this is the solution

这是解决方案

回答by Ivan I?in

In my case, this solved the problem:

就我而言,这解决了问题:

System.Net.ServicePointManager.Expect100Continue = false;

and none of the above.

以上都没有。

回答by dred17

In my case I needed to setup proxy settings to allow not only HTTP but HTTPS on the same port as well, because one of requests was sent by HTTPS protocol.

在我的情况下,我需要设置代理设置以不仅允许 HTTP 还允许同一端口上的 HTTPS,因为其中一个请求是由 HTTPS 协议发送的。

回答by Softec

It was different case for me. Query was taking too long hence connection was timing out. There are five timeouts in WCF 1. Send Timeout - Default 1 min 2. Receive Timeout - Default 1 min 3. Open Timeout - Default 1 min 4. Close Timeout - Default 1 min 5. Inactivity Timeout- Default 10 min

对我来说情况不同。查询耗时太长,因此连接超时。WCF 中有五个超时时间 1. 发送超时 - 默认 1 分钟 2. 接收超时 - 默认 1 分钟 3. 打开超时 - 默认 1 分钟 4. 关闭超时 - 默认 1 分钟 5. 不活动超时 - 默认 10 分钟

I had set Send and Receive time out correctly but problem was due inactivity timeout as query was too long on server, WCF Service was closing channel hence it was failing to transmit back the response. Hope this helps if you are using WCF to get response from server which takes long time to run.

我已经正确设置了发送和接收超时,但问题是由于服务器上的查询时间太长而导致不活动超时,WCF 服务正在关闭通道,因此无法传回响应。如果您使用 WCF 从需要很长时间运行的服务器获取响应,希望这会有所帮助。

回答by Dan Gifford

I had this issue once. My virus protection was the culprit.

我曾经遇到过这个问题。我的病毒防护是罪魁祸首。