响应状态代码400:由于发布大尺寸数据而导致请求错误(httpclient)

时间:2017-07-08 07:49:20

标签: c# asp.net .net async-await dotnet-httpclient

我通过 httpclient 向网络服务方法发送包含文件和字符串的表单,如下所示:

using (FileStream fs = File.Open(FileFullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            using (var client = new HttpClient(new HttpClientHandler { UseProxy = false, Proxy = null }))
            {
                using (var formData = new MultipartFormDataContent())
                {
                    client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
                    //client.MaxResponseContentBufferSize = fs.Length;

                    HttpContent TokenContent = new StringContent(Token);
                    HttpContent fileStreamContent = new StreamContent(fs);

                    formData.Add(TokenContent, "Token");
                    formData.Add(fileStreamContent, Key, currentFile);

                    var response = await client.PostAsync(WebServiceOfServer, formData).ConfigureAwait(false);

                    // equivalent of pressing the submit button on the form
                    if (!response.IsSuccessStatusCode)
                    {
                        result = null;
                    }
                    else
                    {
                        result = response.Content.ReadAsStreamAsync().Result;
                        return true;
                    }
                }
            }
        }

但是,在发送格式大于 2GB 的文件时,响应状态代码为 400 ,其中包含 BadRequest 词组原因。 我设置client.MaxResponseContentBufferSize=fs.Length并且fs.Length大于 2GB ,它会在内部异常处返回错误并显示以下消息:
"不支持超过2147483647个字节的缓冲。\ r \ n参数名称:值\ r \ n实际值为2380364657。" 那? 在web.config中我使用了以下代码:

<httpRuntime maxRequestLength="3145728" requestLengthDiskThreshold="3145728" targetFramework="4.5.1" requestValidationMode="2.0" executionTimeout="999999"/>

<requestLimits maxAllowedContentLength="3221225472"/>


发送大小小于2GB的其他文件时,没有问题。
如何解决我的问题?

1 个答案:

答案 0 :(得分:0)