在ASP.NET中关于分块传输编码的HTTP请求的阅读体

时间:2020-03-05 18:55:04  来源:igfitidea点击:

J2ME客户端正在发送带有分块传输编码的HTTP POST请求。

当ASP.NET(在IIS6和WebDev.exe.server中)尝试读取请求时,它将Content-Length设置为0。我猜这是可以的,因为在加载请求时,Content-length是未知的。

但是,当我读到最后的Request.InputStream时,它返回0。

这是我用来读取输入流的代码。

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

我可以使用Fiddler模​​拟客户的行为,例如

网址
http:// localhost:15148 / page.aspx

标头:
用户代理:提琴手
传输编码:分块
主持人:somesite.com:15148

身体
兔子兔子兔子。感谢光临,它非常有用!

我的身体阅读器从上面返回了一个零长度的字节数组。

有谁知道如何在IIS和ASP.NET开发服务器(cassini)上启用分块编码?

我发现该脚本适用于IIS,但无法正常工作。

解决方案

回答

该url不再起作用,因此很难直接对其进行测试。我想知道这是否行得通,谷歌在bytes.com上找到了一个有经验的人。如果我们再次建立网站,我可以看到它是否真的可以正常工作。

约尔格·乔斯(Joerg Jooss)写道:(为简洁起见略作修改)

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

回答

似乎是官方的:Cassini不支持"传输编码:分块"请求。

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.