Avoiding "connection: close" when returning a 304 (Not Modified) status code in ASP.NET (Part 2)
In part 1 I discussed how to return a HTTP 304 status code when you don’t want to send clients an entire response if the client already has an updated version of the page in their cache.
How can you verify this is actually working as you expect? You can an HTTP monitoring tool to see the requests your browser send and the responses the web server sends back. My favorite tool for this is Fiddler.
Here’s a screen capture of fiddler (click the link to see a larger image):
Notice that the first request we get is a regular HTTP 200 response with the actual data (5 bytes in our case as seen in the body column). The second and third requests return a 304 response as expected.
If you look at the request headers you’ll see the expected If-Modified-Since (upper right pane) :
The Fiddler Timeline add-in provides a graphical layout of the requests, their timing, cache status and connection status. If you’ll go back to the first image you’ll notice that the second and third requests have a floppy icon near them which means these requests were served from the local cache instead of the server. You might also notice a red circle near that floppy.. That’s what this post is all about.
The red circle means the connection will be ended by the server after the response is over. This is also indicated by a “Connection: Close” header that was added to the response (which you can see in image 2 in the lower right pane). Why was the connection closed? The simple answer is because there’s no content-length header in the response. Yeah, so? So, if there’s not content-length header, then the client has no way of knowing when the response is finished and will just keep waiting for more data from the server. Ahh.., but why wasn’t the content-length header added? Well, I have no idea why. What I can tell you is that if your client is going to send another request to your server right after this one, it won’t be able to reuse that connection and will have to open a new one which, in some cases, is just a waste of time. So how do you avoid that? Simple:
context.Response.AddHeader("content-length", "0");
Since we’re not sending any content (That’s what a 304 response is all about), we simply add that header. ASP.NET will now realize that the response is actually empty and there’s no reason to close the connection and will not add that header.
Make A Comment: ( 8 so far )
8 Responses to “Avoiding "connection: close" when returning a 304 (Not Modified) status code in ASP.NET (Part 2)”
Koen
July 22nd, 2008
Hello Eyal,
This is regarding your sample code post on codeproject..
http://www.codeproject.com/KB/cs/IFilter.aspx?msg=3128621
I’m getting following error when I close your tester executable after successfully reading selected PDF file.
—————————————————————————————
.NET-BroadcastEventWindow.2.0.0.0.378734a.0: IFilterTester.exe – Application Error
—————————————————————————————
The instruction at “0x0700609c” referenced memory at “0×00000014″. The memory could not be “read”.
Could you please look into this and help me out.
Thanks in advance.
Regards,
Ganesh.
Ganesh
January 8th, 2010
A server *** should not respond with a Content-Length header on a 304 ***. Please see rfc1945 and rfc2616
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13
http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3
http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
JJ
June 21st, 2010
The client should know that the server is done responding when he sees two newlines after the headers.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
“The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.”
JJ
June 21st, 2010
Buy:Valtrex.Arimidex.Zyban.Mega Hoodia.Retin-A.Accutane.Zovirax.Nexium.Human Growth Hormone.Prednisolone.Prevacid.Petcam (Metacam) Oral Suspension.Actos.Synthroid.100% Pure Okinawan Coral Calcium.Lumigan….
JEREMY
July 22nd, 2010
How do you order the Delphi 2010 version?
Tom
August 19th, 2010
Been trying for quite a while to get a response on how to purchase the Delphi 10 version. Have sent MANY emails, but never get a response.
Tom
August 23rd, 2010
cold http://ucoldyfd.AUTOPARTSVILLE.INFO/tag/Baby+cold+Koi/ : Koi…
cold…
cold
August 30th, 2010



Excellent! Exactly what I was looking for. Setting content-length to 0 did the trick. Thanks!