我正在使用以下代码从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也没有限制。

我已经尝试了几天,以找到一个解决方案,以便任何指针将不胜感激。

谢谢!

最佳答案

在IIS中,http帖子的最大默认大小为4Mg。您必须更改此设置以允许更大的流。

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

这是如何增加此限制的示例。

IIS 7 httpruntime maxRequestLength limit of 2097151

10-07 19:24
查看更多