Q:

HttpWebResponse Async The underlying connection was closed : An unexpected error occured on a send

I m using HttpWebRequest class asynchronously as shown in the code below (it's just windows application):
    private void StartWebRequest(string url)
    {           
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.BeginGetResponse(new AsyncCallback(FinishWebRequest), request);          
    }

    private void FinishWebRequest(IAsyncResult result)
    {
            HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
            Stream responseStream = response.GetResponseStream();
            int num = 100000;
            byte[] buffer = new byte[num];
            int offset = 0;
            while ((num2 = responseStream.Read(buffer, offset, 1000)) != 0)
            {
                offset += num2;
            }
            MemoryStream stream = new MemoryStream(buffer, 0, offset);
            Bitmap bitmap = (Bitmap)Image.FromStream(stream);
            bitmap.Save(@"z:\new.jpg");
            response.Close();

            responseStream.Close();
            stream.Close();

    }

Sometimes I get that error:
The underlying connection was closed : An unexpected error occured on a send

Is there anyway to solve this issue?

A:

You have to put your code in a try and catch block, and if the error is caught try to reconnect again. You may predefine a quota of the number of trials (for example try for 3 times maximum), the network environment is unpredictable, so you have to maintain any unreliability. BTW, after the final trial don't forget to close the streams and the connection

