Avoiding "connection: close" when returning a 304 (Not Modified) status code in ASP.NET (Part 2)

Posted on September 11th, 2007.

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):

Fiddler

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) :

Fiddler2 
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

Make A Comment: ( 8 so far )

blockquote and a tags work here.

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

8 Responses to “Avoiding "connection: close" when returning a 304 (Not Modified) status code in ASP.NET (Part 2)”

RSS Feed for Eyal’s Posts Comments RSS Feed

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

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

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


Medicamentspot.com. Canadian Health&Care.Best quality drugs.Special Internet Prices.No prescription online pharmacy. No prescription drugs. Order drugs online

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
August 30th, 2010

Where's The Comment Form?

eXTReMe Tracker