views:

51

answers:

1

In What conditions are closing a HTTP connection necessary?

+3  A: 

HTTP isn't the type of protocol to have "connections"; it's what they call "stateless", meaning each request is separate from every other request. That's why we have things like session cookies; people had to hack in a way to allow information to be carried over between requests.

Now, even though they're separate, HTTP 1.1 allows a client to make multiple requests over the same TCP/IP connection (which, although it's a connection to an HTTP server, is at a whole other level in the TCP/IP stack). The requests will still be separate, but you don't have to open a new network connection. This allows some efficiency gains, as opening a network connection can be expensive.

If you'd like to take advantage of this, watch the headers in the request and response. If the request is using an HTTP version less than 1.1, or there's a header that says Connection: close, then the connection is to go away after the current request is handled. Otherwise, once it's been cleared out (usually by reading all the data from the previous request), it can be reused.

cHao