Using Apache HttpClient how to set the TIMEOUT on a request and response

I am guessing many people come here because of the title and because the HttpConnectionParams API is deprecated.

Using a recent version of Apache HTTP Client, you can set these timeouts using the request params:

HttpPost request = new HttpPost(url);

RequestConfig requestConfig = RequestConfig.custom()
  .setSocketTimeout(TIMEOUT_MILLIS)
  .setConnectTimeout(TIMEOUT_MILLIS)
  .setConnectionRequestTimeout(TIMEOUT_MILLIS)
  .build();

request.setConfig(requestConfig);

Alternatively, you can also set this when you create your HTTP Client, using the builder API for the HTTP client, but you’ll also need to build a custom connection manager with a custom socket config.

The configuration example file is an excellent resource to find out about how to configure Apache HTTP Client.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)