HttpListener only gets first 900 bytes of InputStream when using jQuery.ajax ()

I have a very simple class using an HttpListener to get HTTP POST from a browser through an AJAX request. I included a demo that highlights this issue:

private static void HandleRequest(HttpListenerContext context)
{
    using (var inputStream = context.Request.InputStream)
    {
        for (int i = 0; i < context.Request.ContentLength64; i++)
        {
            Console.WriteLine("{0}:\t{1}", i, inputStream.ReadByte());
        }
    }
 }

I also use the following Javascript in Firefox (using jQuery):

upload_html: function(html){
   jQuery.ajax({
            type: 'POST',
            data: html,
            url: 'http://localhost:8080/api/html?url=' + escape(content.document.location),
            success: function(data) {
                 // do stuff
            }
   });
}

The problem I am facing is that an InputStream only ever contains about 900 bytes when sent from Firefox. If I put Fiddler between Firefox and my application, it will send all 9,900 bytes correctly. But when sending directly from Firefox, only the first 900 characters are sent to my application (encoded as UTF8, so 900 bytes).

curl , . Fiddler . Fiddler , Firefox, .

html , . , 899 999 . 900 , InputStream.

, , , 1024 , http , HTTP , ~ 120 .

, , , JS Firefox jquery. , , Javascript .

+3
1

ReadToEnd

_RequestBody  = new StreamReader(_Context.Request.InputStream).ReadToEnd();

var memoryStream = new MemoryStream();
var buffer = new byte[0xFFFF];
Stream stream = _Context.Request.InputStream;

int i = 0;
do {
  i = stream.Read(buffer, 0, 0xFFFF);
  memoryStream.Write(buffer, 0, i);
} while (i > 0);
memoryStream.Flush();
memoryStream.Position = 0;
return memoryStream;
0

Source: https://habr.com/ru/post/1758792/


All Articles