Kwah009
Kwah009

Reputation: 203

System.Net.WebException: The underlying connection was closed: 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.

Upvotes: 16

Views: 123470

Answers (9)

jper
jper

Reputation: 41

Faced the same error for using http GET for an API that used https. Might be of help to someone.

Upvotes: 2

Ivan I
Ivan I

Reputation: 9990

In my case, this solved the problem:

System.Net.ServicePointManager.Expect100Continue = false;

and none of the above.

Upvotes: 10

Dan Gifford
Dan Gifford

Reputation: 896

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

Upvotes: 2

Softec
Softec

Reputation: 1157

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

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.

Upvotes: 0

Erik A. Brandstadmoen
Erik A. Brandstadmoen

Reputation: 10608

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.

Upvotes: 3

dred17
dred17

Reputation: 159

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.

Upvotes: 0

Mak
Mak

Reputation: 9

myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;

this is the solution

Upvotes: -1

shamika
shamika

Reputation:

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.

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

Upvotes: 7

Rik
Rik

Reputation: 29243

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 Big files uploading (WebException: The connection was closed unexpectedly)

Upvotes: 4

Related Questions