【问题标题】:Error when sending large data with HTTP Post使用 HTTP Post 发送大数据时出错
【发布时间】:2013-10-09 02:22:47
【问题描述】:

我正在使用以下代码从 WinForms 应用程序向 Web API 发送数据:

private string SendBatch(string URL, string POSTdata)
{
    string responseData = "";
    try
    {
        HttpWebRequest hwrequest = (HttpWebRequest)WebRequest.Create(URL);
        hwrequest.Timeout = 600000;
        hwrequest.KeepAlive = true;
        hwrequest.Method = "POST";
        hwrequest.ContentType = "application/x-www-form-urlencoded";

        byte[] postByteArray = System.Text.Encoding.UTF8.GetBytes("data=" + POSTdata);

        hwrequest.ContentLength = postByteArray.Length;

        System.IO.Stream postStream = hwrequest.GetRequestStream();
        postStream.Write(postByteArray, 0, postByteArray.Length);
        postStream.Close();

        HttpWebResponse hwresponse = (HttpWebResponse)hwrequest.GetResponse();
        if (hwresponse.StatusCode == System.Net.HttpStatusCode.OK)
        {
            System.IO.StreamReader responseStream = new System.IO.StreamReader(hwresponse.GetResponseStream());
            responseData = responseStream.ReadToEnd();
        }
        hwresponse.Close();
    }
    catch (Exception e)
    {
        responseData = "An error occurred: " + e.Message;
    }
    return responseData;

    }
}

当我发送少量数据时,API 接收数据没有任何问题。但是,当我尝试发送大量数据 (30MB+) 时,我从通过格式错误的数据发送的 API 收到错误。

我已将超时设置为 10 分钟,大约 2 分钟后收到错误消息。

根据我在 SO 上遇到的问题,帖子大小没有限制,API 也没有限制。

我已经尝试了几天来寻找解决方案,因此任何指针都将不胜感激。

谢谢!

【问题讨论】:

    标签: c# visual-studio-2010


    【解决方案1】:

    在 IIS 中 http post 默认设置为 4Mg 有一个最大默认大小。您必须更改它以允许更大的流。

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;295626

    以下是如何提高此限制的示例。

    IIS 7 httpruntime maxRequestLength limit of 2097151

    【讨论】:

    • 我不是从网站运行此代码,而是来自 winforms 应用程序。对不起,我应该提到这一点。我知道我可以毫无问题地发送 20MB,所以限制可能在 20MB 到 30MB 之间。
    • 您是从 Win 表单应用程序发布到网站吗?
    • 是的,我是。我已经更新了这个问题。 (对不起,我问的时候忘记加了。)
    • 好的 - 您可以访问该网站吗?是 .Net 可以更改 web.config 吗?是 IIS 吗?
    • 我无法访问该网站,但 API 编码人员说他们有很多人上传更大的文件。
    【解决方案2】:

    如果您拥有站点结帐 IIS 请求限制

    http://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits

    <system.webServer>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="4294967295" />
    

    【讨论】:

      【解决方案3】:

      尝试将HttpWebRequest.SendChunked Property 设置为true。

      【讨论】:

      • 我试了一下。它没有帮助:(
      猜你喜欢
      • 2013-11-18
      • 1970-01-01
      • 2012-12-29
      • 2014-03-25
      • 2020-04-12
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      相关资源
      最近更新 更多