I am trying to send form data in a WebRequest.
The function works fine and returns a 'Success' response stream as expected.
However, If the length of the 'data' variable exceeds past 30,000 characters I am hit with an HTTP 500 error:
Message "The remote server returned an error: (500) Internal Server Error."
Here is my 'Test' function that is sending the data:
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);
}
And here is my 'GetPriceForEntries' function:
[HttpPost]
[DisableRequestSizeLimit]
[Route("GetPriceForEntries")]
public JsonResult GetPricesForEntries([FromForm] List<JsonEntryKey> Entries, bool? ShowAll)
{
return Json("Success");
//return Json(_priceService.GetPriceForEntries(entries));
}
Unfortunately I cannot change how the 'Test' function sends the data. It must remain the same for compatibility as other applications use this function.
Is there a way to increase the size limit a HTTP WebRequest Stream can do or disable the limit?
I am using MVC on the .NET Core btw.
EDIT: Still no luck after adding the following to Web.config:
<security>
<requestFiltering>
<!-- This will handle requests up to 50MB -->
<requestLimits maxAllowedContentLength="52428800" />
</requestFiltering>
</security>
EDIT 2: After further investigation this is the following error I am receiving after reading the WebException response:
<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>
Fixed using the following piece of code above the function to set the form limit.
[RequestFormLimits(ValueCountLimit = int.MaxValue)]
The default limit is 1024.