Auto-Reconnect Feature of Chilkat.Rest Explained

This post is to explain the auto-reconnect argument in the Chilkat.Rest.Connect method.  For example, the Rest.Connect method in C#.

The Chilkat REST class is designed so that your application initially connects
to a particular web server (at a particular port, with or without TLS), and then
one or more requests can be sent.

If the auto-connect argument was set to true, then the REST object will automatically reconnect as needed for each request.

The first request will obviously not need an auto-reconnect because the Connect method was just called successfully.

Web servers will typically keep a client connection open after servicing a request.  This is because when a browser fetches a web page, it will also be fetching the various images, style sheets, Javascripts, etc., which are all on the same server.  All of the GET requests can happen on the same TLS connection.  If the web server closed the connection after each GET, then a web page would take much longer to load.

Web servers typically behave in the same way for REST API requests.  The server will typically keep a connection open for GET requests.  The web server may or may not keep the connection open after a POST, PUT, or DELETE request.   If the auto-reconnect argument was set to true, then your application does not need to worry about whether the server closed the connection or not.  You can simply Rest.Connect once, and then send requests.  If the connection was closed, then Chilkat will automatically reconnect as needed.

Another situation where a reconnect would be needed is for the case where a request is sent on the connection, then a long amount of time elapses before your app sends the next request.  It may be that the server closes the connection after a period of inactivity.  Chilkat.Rest will automatically see that the connection was closed, will reconnect, and will send the request.  Your application should not need to worry about whether the server disconnected or not.

You can test the auto-reconnect feature in one of two ways:

  1. Add a “Connection: close” header to the request.  Do this by calling rest.AddHeader(“Connection”,”close”).  This tells the server to close the connection after the request.  Then send another request (without first calling Connect).  The subsequent request should be successful, and also if you examine the rest.LastErrorText property afterwards, you’ll see information about the new connection that was made.
  2. Wait a long period of time before sending a subsequent request.  You can then examine the LastErrorText to see that a new connection was automatically established.