c# - 使用 HTTP Post 发送大数据时出错

标签 c# visual-studio-2010

我正在使用以下代码将数据从 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

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

IIS 7 httpruntime maxRequestLength limit of 2097151

关于c# - 使用 HTTP Post 发送大数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19261968/

相关文章:

c# - 发送至 iOS 的 PushSharp 通知未到达设备

c# - .NET 反向信号量?

c# - 测试 - 缺少连接字符串

visual-studio-2010 - 我在哪里可以更改 VS10 中的 Asp.net MVC 3 Razor 语法高亮显示?

Visual Studio 2010 中局部变量的 Javascript Intellisense

c# - 为 ASP.NET 网站创建 web.sitemap 文件

sql-server - 为什么 Entity Framework 向导看不到我在 chinook 数据库中的外键?

C# 到 Excel 设置自定义日期显示和值

c# - 哪个执行速度更快或更优化? C#

c# - 使用自定义属性和模型打开 asp.net mvc 窗口