Daniel
Daniel

Reputation: 237

Trouble understanding persistent http connections in c#

I do not quite understand how exactly persistent connections work.

So the keepalive property is set by default and this should keep my connection open, as far as I understand it.

So right now I am sending my data via a POST on an HTTPWebRequest. But I do this everytime I send something to the recipient.

So it is like this: POST data from client to server, response to the post is returned.

But next i just send another POST, instead of using the connection I already opened. So I feel like I am sending more than I actually have to. Can't I just open the connection once and then continue communication via this connection?

I am sorry for my poor understanding of this subject, but this is the first time I really work with network protocols.

Thanks in advance

Daniel

Upvotes: 2

Views: 1680

Answers (2)

O'Rooney
O'Rooney

Reputation: 3080

You can reuse the connection IF the server supports it.... including any proxies in between you and the server. Which is where it sometimes falls down. It was tacked on to HTTP 1.0 and officially added in 1.1.

Basically, your client asks "may I keep this connection alive" with a special header, then if the server supports it, it replies saying "yes" and the client can then send more requests on the same socket.

Your client code also has to support this ability. .Net should be fine.

Upvotes: 0

Aliostad
Aliostad

Reputation: 81660

KeepAlive was added to HTTP protocol to improve server-side performance.

HTTP is generally a stateless protocol. All state is preserved as cookies or server's session. If KeepAlive is set to true, client and the server could potentially keep their underlying TCP Connection alive.

Usually a time-out set for KeepAlive so that if client did not make any other request, this connection is closed.

This feature is usually implemented differently across different platforms, for example I have seen issues with Java implementation where they do not respect the timeout and server closes the connection so client's attempt to connect again fails since it assumes connection is still open.

See RFC document here.

Upvotes: 1

Related Questions