我正在使用以下代码将数据从 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 也没有限制。
几天来,我一直在尝试寻找解决方案,因此我们将不胜感激任何指针。
谢谢!