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

发布于 2024-09-09 04:25:22 字数 1355 浏览 2 评论 0原文

我编写了一个 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 分钟发生故障。有什么办法可以识别是谁关闭了连接吗?我还应该尝试其他什么方法来解决这个问题吗?提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2024-09-16 04:25:22

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

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文