2014-03-06 58 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