更新时间:2022-03-19 23:25:44
根据 ASP.NET 将传输编码设置为在过早刷新响应时分块:
ASP.NET 以分块编码(Transfer-Encoding:chunked)将数据传输到客户端,如果您过早地刷新 Http 请求的响应流并且响应的 Content-Length 标头不是您明确设置的.
ASP.NET transfers the data to the client in chunked encoding (Transfer-Encoding: chunked), if you prematurely flush the Response stream for the Http request and the Content-Length header for the Response is not explicitly set by you.
解决方案:您需要为响应显式设置 Content-Length 标头,以防止 ASP.NET 在刷新时对响应进行分块.
Solution: You need to explicitly set the Content-Length header for the Response to prevent ASP.NET from chunking the response on flushing.
这是我用来防止 ASP.NET 通过设置所需标头来分块响应的 C# 代码:
Here's the C# code that I used for preventing ASP.NET from chunking the response by setting the required header:
protected void writeJsonData (string s) {
HttpContext context=this.Context;
HttpResponse response=context.Response;
context.Response.ContentType = "text/json";
byte[] b = response.ContentEncoding.GetBytes(s);
response.AddHeader("Content-Length", b.Length.ToString());
response.BinaryWrite(b);
try
{
this.Context.Response.Flush();
this.Context.Response.Close();
}
catch (Exception) { }
}