Thomas Thomas - 9 months ago 74
C# Question

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

I have the following code:

private Uri currentUri;

private void Form1_Load(object sender, EventArgs e)
currentUri = new Uri(@"");
HttpWebRequest myRequest = (HttpWebRequest) HttpWebRequest.Create("");
WebProxy myProxy = new WebProxy("");
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

Answer Source

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.


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.