Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

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.
like image 761
Kwah009 Avatar asked Sep 22 '09 11:09

Kwah009


People also ask

How do you solve the underlying connection was closed the connection was closed unexpectedly?

The underlying connection was closed: An unexpected error occurred on a send. This problem usually happens when the computer is using the Proxy/VPN. Please disable the Proxy/VPN temporary and try again.

What is System Net WebException?

The WebException class is thrown by classes descended from WebRequest and WebResponse that implement pluggable protocols for accessing the Internet. When WebException is thrown by a descendant of the WebRequest class, the Response property provides the Internet response to the application.

What is the underlying connection was closed?

A message "underlying connection was closed the connection was closed unexpectedly" would commonly indicate that something in between your BDNA server and the download site has interrupted or disallowed the network connection. For example, network routing may not be allowing your BDNA server to access the Internet.


2 Answers

In my case, this solved the problem:

System.Net.ServicePointManager.Expect100Continue = false;

and none of the above.

like image 53
Ivan Ičin Avatar answered Sep 21 '22 03:09

Ivan Ičin


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

like image 28
Jehan Perera Avatar answered Sep 19 '22 03:09

Jehan Perera