IIS &Chrome:无法加载资源:net::ERR_INCOMPLETE_CHUNKED_ENCODING [英] IIS & Chrome: failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING

查看:58
本文介绍了IIS &Chrome:无法加载资源:net::ERR_INCOMPLETE_CHUNKED_ENCODING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个 Chrome 问题,我认为值得与您分享.

I recently came across a Chrome issue which I think is worth sharing it with you.

我使用 HttpHandler 编写了一个自己编写的 API,主要应该返回 json 数据.但是当发生错误时,我想显示一个 html 文件.这在 IE 和 FF 中效果很好,但在 Chrome 中则不然.

I worked on a self written API using an HttpHandler which primary should return json data. But when an error occures I wanted to display an html file. That worked pretty well in IE and FF, but not in Chrome.

查看开发者工具发现了这个错误:net::ERR_INCOMPLETE_CHUNKED_ENCODING

Looking to the developer tools revealed this error: net::ERR_INCOMPLETE_CHUNKED_ENCODING

谷歌对这个问题说得不多,但看到很多.我只知道它在一段时间后神奇地消失了.

Google said not very much about this issue while it was seen very much. All I got to know was, that it was magically disappearing after some time.

我发现它位于这行代码中:

I found out it lays on this lines of code:

result.StoreResult(context);
context.Response.Flush();
context.Response.Close(); //<-- this causes the error

删除最后一行后,它运行良好.我不知道为什么只有 Chrome 有/有这个问题,但似乎我在 chrome 完成阅读之前关闭了响应流.

After removing the last line it worked well. I don´t know why only Chrome had/has an issue with that, but it seemed as if I closed the response stream before chrome finished reading it.

希望对遇到相同或类似问题的人有所帮助.

I hope it helps those of you coming across the same or a similar issue.

现在我的问题:关闭/刷新响应流的最佳实践是什么?有什么规定吗?

Now my question: How is the best pratice in closing/flushing the response stream? Are there any rules?

推荐答案

根据 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) { }
}

这篇关于IIS &amp;Chrome:无法加载资源:net::ERR_INCOMPLETE_CHUNKED_ENCODING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆