使用 HttpClient post 大型 MultipartFormDataContent 到 IIS 服务器网页

Using HttpClient to post large MultipartFormDataContent to IIS server web page

我正在尝试使用 HttpClient 上传最多 2GB 的数据。 数据通过请求正文发送到一个 aspx 页面并在其中读取(这很糟糕,我知道,但我无法更改。)

数据放在 MultipartFormDataContent 中,post像这样编辑:

var apiRequest = new MultipartFormDataContent();

apiRequest.Add(new StreamContent(file), fileName, fileName);

apiRequest.Headers.ContentDisposition = 
    new ContentDispositionHeaderValue("form-data") { Name = fileName, FileName = fileName };

HttpClient client = new HttpClient();
client.Timeout = TimeSpan.FromMinutes(30);

HttpResponseMessage response = null;

try
{
    response = client.PostAsync(apiEndPoint, form).Result;
    response.EnsureSuccessStatusCode();
    string responseBody = response.Content.ReadAsStringAsync().Result;
}
catch (HttpRequestException e)
{
    log.LogError($"logging here");
}

我尝试过的事情:

-HttpClient http 版本 1.0 而不是默认值

-HttpClient MaxRequestContentBufferSize

-web.config maxAllowedContentLength

-web.config AspMaxRequestEntityAllowed

-web.config 最大请求长度

目前,文件已正确添加到 httpClient,但我无法将它们 post 添加到 Web 应用程序。我达到了 900MB,但超过 900MB 的任何东西都只是重定向到主页,我从响应正文中的 Web 应用程序获得 HTML。

提前致谢!

费尽周折,我们通过查看 IIS 日志找到了解决方案。

日志显示 Microsoft URLScan 工具阻止了请求。 当请求正文未通过扫描时,IIS 会直接将您重定向到主页,不会出现错误。

您必须在 urlscan.ini 文件中配置最大请求长度。 更多信息在这里:https://ajaxuploader.com/large-file-upload-iis-debug.htm

文件位于 C:\WINDOWS\system32\inetsrv\urlscan