1

My ASP.NET app returns JSON object to user, it contains binary encoded data. Due to this I decided to enable HTTP compression and the problem begun with Content-Length.

If I enable compression the Content-Length header is ignored while response s send and connection is not closed immediately. The connection is still open for about 15 seconds after all data has been sent.

I would like to have HTTP compression enabled but don't know how to solve the problem with Content-Length header.

context.Response.AddHeader("Content-Length", jsonObject.ToString().Length.ToString());
context.Response.Write(jsonObject);
context.Response.Flush();
context.Response.Close();
khellang
  • 17,550
  • 6
  • 64
  • 84
Tomas
  • 17,551
  • 43
  • 152
  • 257
  • What's the problem here? Please clarify what you'd like us to help with. – Jeremy McGee Oct 03 '11 at 09:14
  • What is the problem besides that the connection being open for a while? – jgauffin Oct 03 '11 at 10:22
  • The problem is that connection is not closed for about 15 seconds after data is send. This code is used in Online API and if connection is not closed the client stay connected. If Content-Length is added then everything works fine, the connection is closed imidiatlly after data is send. Unfortunatlly Content-Length is ignored on HTTP Compression. How to have Content-Length and HTTP Compression? – Tomas Oct 03 '11 at 10:34

1 Answers1

2

Content-Length represents the length of the data being transferred in your case it is the compressed bytes. See this SO question whose answer links to relevant RFC: content-length when using http compression

In case, HTTP Compression is happening via web server (and not by your code) then I will suggest you to try adding content-length header by your self. Hopefully, web server will add it correctly.

This can be verified using Chrome on http://httpd.apache.org/ if you look at the developer console you would see the Content-Length to be much smaller than the actual uncompressed page in bytes.

Community
  • 1
  • 1
VinayC
  • 47,395
  • 5
  • 59
  • 72