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

10,386

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/

Share:
10,386
Mass Dot Net
Author by

Mass Dot Net

Updated on June 04, 2022

Comments

  • Mass Dot Net
    Mass Dot Net almost 2 years

    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!

  • Mass Dot Net
    Mass Dot Net almost 14 years
    This is a C# Windows service running locally on my machine, and I'm targeting the .NET 3.5 framework (the first link mentioned 1.0 & 1.1). Is there somewhere else on my local PC I should be looking for this setting?
  • Rebecca Chernoff
    Rebecca Chernoff almost 14 years
    Hence the 2nd link which is newer. This applies to the server receiving the upload. Is that a server that you control?
  • Mass Dot Net
    Mass Dot Net almost 14 years
    I have no control over the receiving server -- the only one I have access to is the source server (my local PC).
  • Rebecca Chernoff
    Rebecca Chernoff almost 14 years
    The problem does not sound like it is on your end. I guess maybe test if 4mb is the limit? Is the receiving server even IIS? Do you have a contact for someone on the receiving end?
  • Mass Dot Net
    Mass Dot Net almost 14 years
    I suspect it's on the receiving server's end -- which I doubt the is running Windows, btw. But I wanted to rule out all of the obvious stuff on my end first. I'm going to contact an admin on the receiving end now, but I fear he'll just tell me to look at the settings on my end again.
  • Rebecca Chernoff
    Rebecca Chernoff almost 14 years
    Unless you write up some code to act as the receiving end and test against that, I suppose there's no way to be sure.
  • feroze
    feroze almost 14 years
    Try getting a network log (see ferozedaud.blogspot.com/2009/08/tracing-with-systemnet.html) and see what it shows. Probably the server is closing the connection rudely.
  • Mass Dot Net
    Mass Dot Net almost 14 years
    @feroze: That looks promising. I'll give it a shot this afternoon and report back here.
  • Mass Dot Net
    Mass Dot Net almost 14 years
    @feroze: I think I have the proof I need. Here's the actual point at which the failure occurred, according the trace log file: System.Net.Sockets Error: 0 : [3336] Exception in the Socket#45523402::Receive - An existing connection was forcibly closed by the remote host
  • Mass Dot Net
    Mass Dot Net almost 14 years
    And FWIW, I ended up adding the code from the aforementioned link to machine.config before I got it to work.
  • feroze
    feroze almost 14 years
    I think we know that the server is closing the connection prematurely, but the question is, why? Therefore if you post the whole log to pastebin.com (remove any personally identifiable info, if any, like username/password). we can take a look.