我正在使用以下代码将数据从 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 post 默认设置为 4Mg 的最大默认大小。您必须更改此设置以允许更大的流。
http://support.microsoft.com/default.aspx?scid=kb;EN-US;295626
这是一个如何增加此限制的示例。
关于c# - 使用 HTTP Post 发送大数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19261968/