C# WebRequest 发送表单数据大小限制
C# WebRequest Send Form Data Size Limit
我正在尝试在 WebRequest 中发送表单数据。
该函数工作正常,returns 'Success' 响应流符合预期。
但是,如果 'data' 变量的长度超过 30,000 个字符,我将遇到 HTTP 500 错误:
Message "The remote server returned an error: (500) Internal Server Error."
这是我的 'Test' 发送数据的函数:
public IActionResult Test()
{
var data = "[0].meetingDate=2019-07-17&[0].courseId=9&
[0].raceNumber=1&[0].horseCode=000000500523";
//THIS MAKES THE REQUEST ERROR BY INCREASING THE SIZE OF THE DATA WHICH THEN GENERATES A ERROR
for( int i = 0; i < 400; i++)
{
data = data + "[0].meetingDate=2019-07-17&[0].courseId=9&[0].raceNumber=1&[0].horseCode=000000500523";
}
//////////////////////////
WebRequest request = WebRequest.Create("https://localhost:44374/HorseRacingApi/Prices/GetPriceForEntries");
request.Method = "POST";
if (!string.IsNullOrEmpty(data))
{
byte[] dataBytes = Encoding.ASCII.GetBytes(data);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = dataBytes.Length;
using (Stream requestDataStream = request.GetRequestStream())
{
requestDataStream.Write(dataBytes, 0, dataBytes.Length);
requestDataStream.Flush();
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream);
Debug.WriteLine("Response stream received.");
string responseMsg = readStream.ReadToEnd();
Debug.WriteLine(responseMsg);
response.Close();
readStream.Close();
return Ok(responseMsg);
}
这是我的 'GetPriceForEntries' 函数:
[HttpPost]
[DisableRequestSizeLimit]
[Route("GetPriceForEntries")]
public JsonResult GetPricesForEntries([FromForm] List<JsonEntryKey> Entries, bool? ShowAll)
{
return Json("Success");
//return Json(_priceService.GetPriceForEntries(entries));
}
不幸的是,我无法更改 'Test' 函数发送数据的方式。它必须与其他应用程序使用此功能时的兼容性保持相同。
有没有办法增加 HTTP WebRequest 流的大小限制或禁用该限制?
顺便说一句,我在 .NET Core 上使用 MVC。
编辑:将以下内容添加到 Web.config 后仍然没有运气:
<security>
<requestFiltering>
<!-- This will handle requests up to 50MB -->
<requestLimits maxAllowedContentLength="52428800" />
</requestFiltering>
</security>
编辑 2:经过进一步调查,这是我在阅读 WebException 响应后收到的以下错误:
<h1>An unhandled exception occurred while processing the request.</h1>
<div class="titleerror">InvalidDataException: Form value count limit 1024 exceeded.</div>
<p class="location">Microsoft.AspNetCore.WebUtilities.FormReader.Append(ref KeyValueAccumulator accumulator)</p>
修复了在函数上方使用以下代码设置表单限制的问题。
[RequestFormLimits(ValueCountLimit = int.MaxValue)]
默认限制为 1024。
我正在尝试在 WebRequest 中发送表单数据。
该函数工作正常,returns 'Success' 响应流符合预期。
但是,如果 'data' 变量的长度超过 30,000 个字符,我将遇到 HTTP 500 错误:
Message "The remote server returned an error: (500) Internal Server Error."
这是我的 'Test' 发送数据的函数:
public IActionResult Test()
{
var data = "[0].meetingDate=2019-07-17&[0].courseId=9&
[0].raceNumber=1&[0].horseCode=000000500523";
//THIS MAKES THE REQUEST ERROR BY INCREASING THE SIZE OF THE DATA WHICH THEN GENERATES A ERROR
for( int i = 0; i < 400; i++)
{
data = data + "[0].meetingDate=2019-07-17&[0].courseId=9&[0].raceNumber=1&[0].horseCode=000000500523";
}
//////////////////////////
WebRequest request = WebRequest.Create("https://localhost:44374/HorseRacingApi/Prices/GetPriceForEntries");
request.Method = "POST";
if (!string.IsNullOrEmpty(data))
{
byte[] dataBytes = Encoding.ASCII.GetBytes(data);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = dataBytes.Length;
using (Stream requestDataStream = request.GetRequestStream())
{
requestDataStream.Write(dataBytes, 0, dataBytes.Length);
requestDataStream.Flush();
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream);
Debug.WriteLine("Response stream received.");
string responseMsg = readStream.ReadToEnd();
Debug.WriteLine(responseMsg);
response.Close();
readStream.Close();
return Ok(responseMsg);
}
这是我的 'GetPriceForEntries' 函数:
[HttpPost]
[DisableRequestSizeLimit]
[Route("GetPriceForEntries")]
public JsonResult GetPricesForEntries([FromForm] List<JsonEntryKey> Entries, bool? ShowAll)
{
return Json("Success");
//return Json(_priceService.GetPriceForEntries(entries));
}
不幸的是,我无法更改 'Test' 函数发送数据的方式。它必须与其他应用程序使用此功能时的兼容性保持相同。
有没有办法增加 HTTP WebRequest 流的大小限制或禁用该限制?
顺便说一句,我在 .NET Core 上使用 MVC。
编辑:将以下内容添加到 Web.config 后仍然没有运气:
<security>
<requestFiltering>
<!-- This will handle requests up to 50MB -->
<requestLimits maxAllowedContentLength="52428800" />
</requestFiltering>
</security>
编辑 2:经过进一步调查,这是我在阅读 WebException 响应后收到的以下错误:
<h1>An unhandled exception occurred while processing the request.</h1>
<div class="titleerror">InvalidDataException: Form value count limit 1024 exceeded.</div>
<p class="location">Microsoft.AspNetCore.WebUtilities.FormReader.Append(ref KeyValueAccumulator accumulator)</p>
修复了在函数上方使用以下代码设置表单限制的问题。
[RequestFormLimits(ValueCountLimit = int.MaxValue)]
默认限制为 1024。