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

守給你的承諾、 提交于 2020-01-14 13:44:11

问题


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.


回答1:


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.




回答2:


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());
}


来源:https://stackoverflow.com/questions/69104/reading-body-on-chunked-transfer-encoded-http-requests-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!