System.Net.WebException:基础连接已经关闭:连接被意外关闭 [英] System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly

查看:7904
本文介绍了System.Net.WebException:基础连接已经关闭:连接被意外关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写这是应该将数据发布到另一个.NET应用程序.NET应用程序。
我用下面的code,请求登录页面

  WebProxy代理=新WebProxy(HTTP://代理:80 /,真正的);
HttpWebRequest的WebRequest的= WebRequest.Create(LOGIN_URL)为HttpWebRequest的;
//proxy.Credentials =新的NetworkCredential(名为myusername,输入mypassword,域名);
// webRequest.Proxy =代理;
webRequest.Proxy = WebRequest.DefaultWebProxy;StreamReader的responseReader =新的StreamReader
                                  (webRequest.GetResponse()GetResponseStream());
字符串responseData = responseReader.ReadToEnd();

但它在这一行失败

  StreamReader的responseReader =新的StreamReader
                                  (webRequest.GetResponse()GetResponseStream());

与错误消息:

  System.Net.WebException:基础连接已经关闭:连接是
                         意外关闭。


解决方案

我前一段时间遇到了同样的异常,我记得,发生这种情况的在某些情况下的由于.NET的错误。您可以解决此通过请求超时和ReadWriteTimeout设定为较高的值,或者设置的KeepAlive为false。

这也只是一种变通方法,虽然如此,我建议你试试假设任何事情之前找到实际的根本原因。

我会尽力拿出一些Web引用,在平均时间,看看
<一href=\"http://stackoverflow.com/questions/1060966/big-files-uploading-webexception-the-connection-was-closed-unexpectedly\">http://stackoverflow.com/questions/1060966/big-files-uploading-webexception-the-connection-was-closed-unexpectedly

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

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.

解决方案

I encountered the same exception a while ago and I remember that this happens in some cases due 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.

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 http://stackoverflow.com/questions/1060966/big-files-uploading-webexception-the-connection-was-closed-unexpectedly

这篇关于System.Net.WebException:基础连接已经关闭:连接被意外关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆