23

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

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.

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

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?

christoph
  • 1,019
  • 1
  • 12
  • 23
  • Check this resource [Response.End, Response.Close, and How Customer Feedback Helps Us Improve MSDN Documentation](http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx); I have the same issue trying to send a Chunked response, maybe your response is chunked (by default). – Lorenzo Solano Martinez Mar 28 '14 at 22:18
  • I have exactly the same issue with a local JSON file wrapped in a JSONP callback. It happens also when I request the file from a remote CDN. – And Finally Mar 31 '14 at 09:32
  • 2
    In my case, I had the `net::ERR_INCOMPLETE_CHUNKED_ENCODING` error because the network cable of the server wasn't fully connected. – falsarella Apr 10 '14 at 19:08
  • I recently found out that my Kaspersky was blocking responses over 2MB, and that caused this error. I had to pause kaspersky protection for the page to load correctly. – Henriksjodahl Apr 25 '14 at 08:23
  • did you tried and get any final solution ? – Kiquenet May 25 '16 at 12:49

6 Answers6

17

According to 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.

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) { }
}
Community
  • 1
  • 1
rumis
  • 197
  • 1
  • 6
  • I have not solution yet. `Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING` _References:_ http://stackoverflow.com/questions/37434368/failed-to-load-resource-neterr-incomplete-chunked-encoding-in-ie-asp-net and http://forums.asp.net/p/2095940/6054370.aspx?p=True&t=635998128824286564 - Problems maybe Content-Length in Chrome using ***UpdatePanels***. I don’t know. – Kiquenet May 26 '16 at 06:43
  • I had the same problem in `IExceptionFilter` and setting `Content-Length` helped! Thanks – andrew.fox May 31 '21 at 17:06
10

I was running into this error when generating a file and pushing it to the user for download, but only occasionally. When it didn't fail, the file was consistently 2 bytes short. Close() forcibly closes the connection, whether it's finished or not, and in my case it was not. Leaving it out, as suggested in the question, meant the resulting file contained both the generated content as well as the HTML for the entire page.

The solution here was replacing

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

with

context.Response.End();

which does the same, but without cutting the transaction short.

Pawtuxet
  • 101
  • 4
  • 4
2

In my case, the problem was cache-related and was happening when doing a CORS request.

Forcing the response header Cache-Control to no-cache resolved my issue:

[ using Symfony HttpFoundation component ]

<?php
$response->headers->add(array(
   'Cache-Control' => 'no-cache'
));
eightyfive
  • 4,601
  • 3
  • 35
  • 44
1

I was also getting same error. This issue was with web server user permission on cache folder.

Vinay Shankar
  • 19
  • 1
  • 3
1

On the offchance that someone is landing here as a result of issues with their ASP.net Core project, I was able to resolve by adding the IIS middleware.

This is done by adding UseIISIntegration when instantiating your webhost instance.

Alexander Trauzzi
  • 7,277
  • 13
  • 68
  • 112
  • 1
    could you be more elaborate on this. ive already added UseIISIntegration but i still get this error. do i need to config any option? – Amir Jalali May 22 '17 at 18:10
  • I was going to look into this solution also but first link no longer exists and the second is Microsoft Docs explaining that the `CreateDefaultBuilder()` already calls `UseIISIntegration`. So I don't know if that means the above is already happening in .Net Core or what because my `program.cs` creates the default builder as explained. – Daniel Jackson Feb 25 '19 at 17:30
1

Once I had the same problem and the main reason was lying in my controller return type. If you try to return a C# object just as-is, you will only get net::ERR_INCOMPLETE_CHUNKED_ENCODING so don't forget to serialize your complex objects before sending them out for java script client (or View). i.e. my controller return type was :

public async Task<List<ComplexModel>> GetComplexModelList(){
    return new List<ComplexModel>()
}

Which caused INCOMPLETE_CHUNKED_ENCODING error, so I tried to fix my mistake with something like:

using Newtonsoft.Json;
...
public async Task<string> GetComplexModelList(){
    return JsonConvert.SerializeObject(new List<ComplexModel>())
}
AmiNadimi
  • 5,129
  • 3
  • 39
  • 55