問題描述
在 ASP.NET 中讀取分塊傳輸編碼的 http 請求的正文 (Reading Body on chunked transfer encoded http requests in ASP.NET)
分塊主機:somesite.com:15148</p>
Body rabbits rabbits rabbits rabbits。謝謝你的到來,它非常有用!
我上面的正文閱讀器將返回一個零長度字節數組......蹩腳......
有誰知道如何啟用分塊編碼在 IIS 和 ASP.NET 開發服務器 (cassini) 上?
我發現 此腳本 用於 IIS,但它不起作用。
參考解法
方法 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());
}
(by CVertex、Anton Tykhyy、Andrew)