2014-03-06 110 views
19

我最近遇到了一个Chrome问题,我认为它值得与您分享。IIS和Chrome:无法加载资源:net :: ERR_INCOMPLETE_CHUNKED_ENCODING

我使用HttpHandler编写了一个自编写的API,其中主要应该返回json数据。但是当发生错误时,我想显示一个html文件。这在IE和FF中运行得非常好,但不是在Chrome中。

展望开发工具揭示了这个错误:净:: ERR_INCOMPLETE_CHUNKED_ENCODING

谷歌表示并不非常关注这个问题,而这被看作非常多。我所知道的是,它在一段时间后神奇地消失了。

我发现它奠定了在这个代码行:

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

除去最后效果不错的行之后。我不知道为什么只有Chrome浏览器有这个问题,但似乎我在Chrome浏览器完成阅读之前关闭了响应流。

我希望它能帮助那些遇到相同或类似问题的人。

现在我的问题: 关闭/刷新响应流的最佳实践是怎样的?有任何规则吗?

+0

检查此资源[Response.End,Response.Close和客户反馈如何帮助我们改进MSDN文档](http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-最终响应近距离和知识,客户反馈,帮助-US-提高-MSDN-documentation.aspx);我有同样的问题尝试发送一个Chunked响应,也许你的响应被分块(默认情况下)。 –

+0

我有一个与JSONP回调中包装的本地JSON文件完全相同的问题。它也发生在我从远程CDN请求文件时。 –

+1

在我的情况下,我有'net :: ERR_INCOMPLETE_CHUNKED_ENCODING'错误,因为服务器的网线没有完全连接。 – falsarella

回答

9

根据ASP.NET sets the transfer encoding as chunked on premature flushing the Response

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.

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#代码:

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) { } 
} 
+0

我还没有解决。 '加载资源失败:net :: ERR_INCOMPLETE_CHUNKED_ENCODING' _参考:_ http://stackoverflow.com/questions/37434368/failed-to-load-resource-neterr-incomplete-chunked-encoding-in-ie-asp- net和 http://forums.asp.net/p/2095940/6054370.aspx?p=True&t=635998128824286564 - 问题可能是Content-Length在Chrome中使用*** UpdatePanels ***。我不知道。 – Kiquenet

1

在我的情况下,问题与缓存相关并且在执行CORS请求时发生。

强制响应报头Cache-Controlno-cache解决我的问题:

[使用的Symfony HttpFoundation成分]

<?php 
$response->headers->add(array(
    'Cache-Control' => 'no-cache' 
)); 
1

我也得到同样的错误。此问题与缓存文件夹的Web服务器用户权限有关。

9

生成文件并将其推送给用户进行下载时,我偶然遇到了此错误。当它没有失败时,文件一直是2个字节。关闭()强制关闭连接,无论是否完成,在我的情况下,它不是。正如问题中所建议的那样,将其排除在外意味着生成的文件包含生成的内容以及整个页面的HTML。

这里将溶液用

context.Response.End(); 

其做同样的替换

context.Response.Flush(); 
context.Response.Close(); 

,但不切断该事务短。

1

由于与他们的ASP.net核心项目有关的问题导致某人在此登陆,我能够通过adding the IIS middleware解决。

这是通过adding UseIISIntegration在实例化您的虚拟主机实例时完成的。

+0

你可以更详细说明这一点。我已经添加了UseIISIntegration,但我仍然得到这个错误。我需要配置任何选项吗? – 1AmirJalali