Basic HttpClient Configuration Example

In this post we will provide a basic HttpClient configuration example. The example will configure different timeout values along with connection pool management.

HttpClient configuration example:

The example below configures a HttpClient object. The configuration details is as follows:

  • RequestConfig: This object is an immutable class which encapsulates request configuration items.
  • RequestConfig.setConnectionRequestTimeout(): Timeout in milliseconds when requesting a connection from the connectionManager. Setting 0 is interpreted as infinite timeout.
  • RequestConfig.setConnectTimeout(): Timeout in milliseconds until a connection has been established (to remote host you are calling). Again, setting 0 is interpreted as infinite timeout.
  • RequestConfig.setSocketTimeout(): Timeout in milliseconds waiting for data to arrive from remote host (i.e. the period of inactivity between 2 consecutive data packets.)
  • PoolingHttpClientConnectionManager: As the name suggests this handles a pool of HttpClientConnections which will be used by multiple client threads. Connections will be pooled on a per route basis.
  • PoolingHttpClientConnectionManager.setDefaultMaxPerRoute(): This specifies the maximum number of connections per route. Default is 2
  • PoolingHttpClientConnectionManager.setMaxTotal():  This specifies the maximum number of connection across all routes.

Similar posts:

  1. Java HttpClient Post Example
  2. Java HttpClient GET Example
  3. Java HttpClient PUT example


  1. RequestConfig API Documentation
  2. PoolingHttpClientConnectionManager API Documentation
  3. HttpClient Overview

Leave a Reply