Reading Body on chunked transfer encoded http requests in ASP.NET
Asked Answered
P

2

5

A J2ME client is sending HTTP POST requests with chunked transfer encoding.

When ASP.NET (in both IIS6 and WebDev.exe.server) tries to read the request it sets the Content-Length to 0. I guess this is ok because the Content-length is unknown when the request is loaded.

However, when I read the Request.InputStream to the end, it returns 0.

Here's the code I'm using to read the input stream.

using (var reader = new StreamReader(httpRequestBodyStream, BodyTextEncoding)) {
    string readString = reader.ReadToEnd();
    Console.WriteLine("CharSize:" + readString.Length);
    return BodyTextEncoding.GetBytes(readString);
}

I can simulate the behaiviour of the client with Fiddler, e.g.

URL http://localhost:15148/page.aspx

Headers: User-Agent: Fiddler Transfer-Encoding: Chunked Host: somesite.com:15148

Body rabbits rabbits rabbits rabbits. thanks for coming, it's been very useful!

My body reader from above will return a zero length byte array...lame...

Does anyone know how to enable chunked encoding on IIS and ASP.NET Development Server (cassini)?

I found this script for IIS but it isn't working.

Pomade answered 16/9, 2008 at 3:11 Comment(0)
W
2

Seems to be official: Cassini does not support Transfer-Encoding: chunked requests.

By default, the client sends large binary streams by using a chunked HTTP Transfer-Encoding. Because the ASP.NET Development Server does not support this kind of encoding, you cannot use this Web server to host a streaming data service that must accept large binary streams.

Wrathful answered 27/11, 2010 at 21:34 Comment(0)
S
1

That url does not work any more, so it's hard to test this directly. I wondered if this would work, and google turned up someone who has experience with it at bytes.com. If you put your website up again, I can see if this really works there.

Joerg Jooss wrote: (slightly modified for brevity )

string responseText = null;
WebRequest rabbits= WebRequest.Create(uri);
using (Stream resp = rabbits.GetResponse().GetResponseStream()) {
    MemoryStream memoryStream = new MemoryStream(0x10000);
    byte[] buffer = new byte[0x1000];
    int bytes;
    while ((bytes = resp.Read(buffer, 0, buffer.Length)) > 0) {
        memoryStream.Write(buffer, 0, bytes);
    }
    // use the encoding to match the data source.
    Encoding enc = Encoding.UTF8;
    reponseText = enc.GetString(memoryStream.ToArray());
}
Sandisandidge answered 17/9, 2008 at 6:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.