HttpWebRequest.GetResponse: “The underlying connection was closed: An unexpected error occurred on a receive.”

StackOverflow https://stackoverflow.com/questions/3197010

Question

I've written a C# Windows service (.NET Framework 3.5, C# 3.0) that posts files & HTML form information to a remote server, and then stores the XML server response in a database. Here is the main chunk of pertinent code:

    HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;

    request.ProtocolVersion = HttpVersion.Version10;
    request.KeepAlive = false;
    request.Timeout = 600000;
    request.ReadWriteTimeout = 600000;
    request.Method = "POST";
    request.ContentType = contentType;
    request.UserAgent = userAgent;
    request.CookieContainer = new CookieContainer();
    request.ContentLength = formData.Length;

    using (Stream requestStream = request.GetRequestStream())
    {
        // Push it out there
        requestStream.Write(formData, 0, formData.Length);
        requestStream.Close();
    }

    return request.GetResponse() as HttpWebResponse;

My service works properly for all small files, but I get the following error when I try to send larger files (8-9 MB).

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

I looked at the outgoing request using Fiddler, and was able to glean the following info:

    HTTP/1.1 504 Fiddler - Receive Failure
    Content-Type: text/html
    Connection: close
    Timestamp: 12:25:04.067

    ReadResponse() failed: The server did not return a response for this request.

The failure occurs ~7 minutes after I call request.GetResponse(). Is there any way to identify who shut down the connection? And is there anything else I should try on my end to resolve this issue? Thanks in advance!

Was it helpful?

Solution

Since you mention it working for small files, but not larger, I'd suggest checking the max file upload size on the server. I believe the default is 4mb. http://support.microsoft.com/kb/295626

EDIT: Noticed the link above is somewhat out of date. Here's one for iis7: http://www.cyprich.com/2008/06/19/fixing-file-upload-size-limit-in-iis-7/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top