2009-09-22 242 views
6

我正在编写一个.NET应用程序,该应用程序应该将数据发布到另一个.NET应用程序。 我使用下面的代码来请求登录页面System.Net.WebException:底层连接已关闭:连接意外关闭

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

,但它在这一行

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

与错误消息失败:

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

开始追查这个问题,我建议你在包装异常捕获代码调用,并打印出完整的堆栈跟踪。 – 2009-09-22 11:27:33

+1

这个例外特别难以追查。如果您想要任何合理的答案,请尽可能多地提供信息 – Rik 2009-09-22 11:31:41

+0

来自例外的堆栈跟踪只能提供以下内容: at System.Net.HttpWebRequest.GetResponse() – Kwah009 2009-09-22 11:41:45

回答

2

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

虽然这只是一种解决方法,所以我建议您在采取任何措施之前尝试找到实际的根本原因。

我会尽力拿出一些Web引用,在平均时间,看看 Big files uploading (WebException: The connection was closed unexpectedly)

+0

看起来像我们在这里的东西。我现在收到错误消息远程服务器返回一个错误:(411)长度必需。虽然当我设置应用程序挂起lengthfeild ... – Kwah009 2009-09-22 12:25:33

0

好像到可能出现的问题:

  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

-1
myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials; 

这是解决方案

3

在我而言,这解决了这个问题:

   System.Net.ServicePointManager.Expect100Continue = false; 

和以上皆非。

+0

我有一个想传达给嵌入式设备的Web服务的问题,这是解决方案。 – jamesallman 2014-10-25 15:17:39

0

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

0

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

我已经正确设置发送和接收超时,未能传回回应。 希望这有助于如果您使用WCF从服务器获取响应,需要很长时间才能运行。

0

我有这个问题一次。我的病毒防护是罪魁祸首。

相关问题