HttpWebRequest.GetResponse:“基础连接已关闭:接收时发生意外错误。”

时间:2010-07-07 16:49:41

标签: c# httpwebrequest timeout getresponse

我编写了一个C#Windows服务(.NET Framework 3.5,C#3.0),用于发布文件和文件。 HTML表单信息到远程服务器,然后将XML服务器响应存储在数据库中。以下是相关代码的主要部分:

    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;

我的服务适用于所有小文件,但是当我尝试发送更大的文件(8-9 MB)时出现以下错误。

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

我使用Fiddler查看了传出请求,并且能够收集以下信息:

    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.

在我打电话 request.GetResponse()后7分钟发生故障。有没有办法确定谁关闭了连接?还有什么我应该尝试解决这个问题吗?提前谢谢!

1 个答案:

答案 0 :(得分:2)

既然你提到它适用于小文件,但不是更大,我建议检查服务器上的最大文件上传大小。我相信默认值是4mb。 http://support.microsoft.com/kb/295626

编辑:注意上面的链接有点过时了。这是iis7的一个:http://www.cyprich.com/2008/06/19/fixing-file-upload-size-limit-in-iis-7/