Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

..The underlying connection was closed: An unexpected error occurred on a receive

Tags:

c#

I have the following code:

private Uri currentUri;  private void Form1_Load(object sender, EventArgs e) {     currentUri = new Uri(@"http://www.stackoverflow.com");     HttpWebRequest myRequest = (HttpWebRequest) HttpWebRequest.Create("http://www.stackoverflow.com");     WebProxy myProxy = new WebProxy("120.198.230.8:81");     myRequest.Proxy = myProxy;      HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();      webBrowser1.DocumentStream = myResponse.GetResponseStream();      webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating); }  void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) {     if (e.Url.AbsolutePath != "blank")     {         currentUri = new Uri(currentUri, e.Url.AbsolutePath);         HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(currentUri);          HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();          webBrowser1.DocumentStream = myResponse.GetResponseStream();         e.Cancel = true;     } } 

after compiling:

error: An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The underlying connection was closed: An unexpected error occurred on a receive.

at line HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

Please help me.

like image 537
Thomas Avatar asked Feb 12 '14 13:02

Thomas


People also ask

What does the underlying connection was closed mean?

The underlying connection was closed: An unexpected error occurred on a receive. This problem occurs when the server or another network device unexpectedly closes an existing Transmission Control Protocol (TCP) connection.


2 Answers

Setting the HttpWebRequest.KeepAlive to false didn't work for me.

Since I was accessing a HTTPS page I had to set the Service Point Security Protocol to Tls12.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

Notice that there are other SecurityProtocolTypes: SecurityProtocolType.Ssl3, SecurityProtocolType.Tls, SecurityProtocolType.Tls11

So if the Tls12 doesn't work for you, try the three remaining options.

Also notice that you can set multiple protocols. This is preferable on most cases.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; 

Edit: Since this is a choice of security standards it's obviously best to go with the latest (TLS 1.2 as of writing this), and not just doing what works. In fact, SSL3 has been officially prohibited from use since 2015 and TLS 1.0 and TLS 1.1 will likely be prohibited soon as well. source: @aske-b

like image 200
Bartho Bernsmann Avatar answered Sep 20 '22 23:09

Bartho Bernsmann


The underlying connection was closed: An unexpected error occurred on a receive.

This problem occurs when the server or another network device unexpectedly closes an existing Transmission Control Protocol (TCP) connection. This problem may occur when a time-out value on the server or on the network device is set too low. To resolve this problem, see resolutions A, D, E, F, and O. The problem can also occur if the server resets the connection unexpectedly, such as if an unhandled exception crashes the server process. Analyze the server logs to see if this may be the issue.

Resolution

To resolve this problem, make sure that you are using the most recent version of the .NET Framework.

Add a method to the class to override the GetWebRequest method. This change lets you access the HttpWebRequest object. If you are using Microsoft Visual C#, the new method must be similar to the following.

class MyTestService:TestService.TestService {     protected override WebRequest GetWebRequest(Uri uri)     {         HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri);         //Setting KeepAlive to false         webRequest.KeepAlive = false;         return webRequest;     } } 

Excerpt from KB915599: You receive one or more error messages when you try to make an HTTP request in an application that is built on the .NET Framework 1.1 Service Pack 1.

like image 31
Nagaraj S Avatar answered Sep 21 '22 23:09

Nagaraj S