33

I am using Http Apache Components to perform the http interactions. I need to adjust my http client. For this purpose I have two parameters: connection timeout and connection request timeout. In library documentation and in source code(no comments were found) I didn't found definition of this terms. I need to know what do they exactly mean. May be they were defined in HTTP protocol documentation but I can't find it. So, my question is what do this two terms mean and how they distinct from each other.

Jonik
  • 80,077
  • 70
  • 264
  • 372
mvb13
  • 1,514
  • 3
  • 18
  • 33

2 Answers2

35

HttpClient has a way to set connection and socket timeout (setConnectionTimeout() and setTimeout()) according to the HttpClient javadocs.

Connection timeout is the timeout until a connection with the server is established.

Socket timeout is the timeout to receive data (socket timeout).

Example:

Let's say you point your browser to access a web page. If the server does not anwser in X seconds, a connection timeout will occur. But if it establishes the connection, then the server will start to process the result for the browser. If it does not ends this processing in Y seconds, a socket timeout will occur.

Gray
  • 115,027
  • 24
  • 293
  • 354
dgimenes
  • 892
  • 11
  • 23
  • 7
    Thank you for your reply, but still I have a problem. Here is request config builder http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/apidocs/org/apache/http/client/config/RequestConfig.Builder.html and it has three methods setConnectionRequestTimeout, setConnectTimeout, setSocketTimeout(and no comments!). So I still don't know what exactly each method does and what methods I should to use. – mvb13 Nov 29 '13 at 07:04
  • 40
    Ok, I finally found that getConnectionRequestTimeout returns imeout in milliseconds used when requesting a connection from the connection manager. I guess that connection manager is apache-httpcomponents library class, so I will not use it. I will use setConnectionTimeout() and setTimeout(). – mvb13 Nov 29 '13 at 07:22
  • 5
    This is a case where the comment is more helpful than the answer. – Jerry Chin May 23 '19 at 09:06
  • 2
    The answer does not tell us what the connection request timeout is. Had to check the comments. – beinghuman Jun 27 '20 at 00:52
15

From the documentation:

/**
 * Returns the timeout in milliseconds used when requesting a connection
 * from the connection manager. A timeout value of zero is interpreted
 * as an infinite timeout.
 * <p>
 * A timeout value of zero is interpreted as an infinite timeout.
 * A negative value is interpreted as undefined (system default).
 * </p>
 * <p>
 * Default: {@code -1}
 * </p>
 */
public int getConnectionRequestTimeout() {
    return connectionRequestTimeout;
}

/**
 * Determines the timeout in milliseconds until a connection is established.
 * A timeout value of zero is interpreted as an infinite timeout.
 * <p>
 * A timeout value of zero is interpreted as an infinite timeout.
 * A negative value is interpreted as undefined (system default).
 * </p>
 * <p>
 * Default: {@code -1}
 * </p>
 */
public int getConnectTimeout() {
    return connectTimeout;
}

This is how the code should look:

HttpClientBuilder clientBuilder = HttpClientBuilder.create();
RequestConfig.Builder requestBuilder = RequestConfig.custom();
// Connection Timeout to establish a connection
requestBuilder = requestBuilder.setConnectTimeout(connectTimeoutMillis);
// Timeout to get a connection from the connection manager for the Http Client
requestBuilder = requestBuilder.setConnectionRequestTimeout(requestTimeoutMillis);
// Timeout between two data packets from the server
requestBuilder = requestBuilder.setSocketTimeout(requestTimeoutMillis);
clientBuilder.setDefaultRequestConfig(requestBuilder.build());
CloseableHttpClient httpClient = clientBuilder.build();
Raj Hassani
  • 1,577
  • 1
  • 19
  • 26
  • 1
    this answer is at least talking about what was asked, unlike the accepted one. however, in which case would requesting a connection from connection manager time out? what are the implications of setting it? when should you set it? It's really quite unclear. – eis Apr 29 '21 at 09:52
  • The connection manager could be a pool like `PoolingHttpClientConnectionManager`. When all connections from the pool are used, then the `ConnectionRequestTimeout` indicates how long your code should wait for a connection to be freed up. – Chris Sekas Jul 13 '21 at 14:34