c# - IIS & Chrome: failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING -


i came across chrome issue think worth sharing you.

i worked on self written api using httphandler primary should return json data. when error occures wanted display html file. worked pretty in ie , ff, not in chrome.

looking developer tools revealed error: net::err_incomplete_chunked_encoding

google said not issue while seen much. got know was, magically disappearing after time.

i found out lays on lines of code:

result.storeresult(context); context.response.flush(); context.response.close(); //<-- causes error 

after removing last line worked well. don´t know why chrome had/has issue that, seemed if closed response stream before chrome finished reading it.

i hope helps of coming across same or similar issue.

now question: how best pratice in closing/flushing response stream? there rules?

according asp.net sets transfer encoding chunked on premature flushing response:

asp.net transfers data client in chunked encoding (transfer-encoding: chunked), if prematurely flush response stream http request , content-length header response not explicitly set you.

solution: need explicitly set content-length header response prevent asp.net chunking response on flushing.

here's c# code used preventing asp.net chunking response setting 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) { } } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -