问题
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.
回答1:
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.
回答2:
So, my question is what do [connection-timeout and connection-request-timeout] mean and how they distinct from each other.
Connection-timeout is the timeout in milliseconds until the server accepts the request. If you specify 3000, the http-client will wait 3 seconds for the server to accept the TCP connection before timing out. This typically is used to make sure you don't have a networking issue or that you are contacting the right hostname or address. This is equivalent to curl's --connect-timeout seconds
option.
Connection-request-timeout is the input/output timeout after the connection has been established. If you specify this value as 10000 then after the http-client has connected to the server and sends a request, it will wait 10 seconds for the server to return a result. This typically is used to ensure that your job doesn't wait for a slow server forever. This is equivalent to curl's --max-time seconds
option.
In HttpClient 4.X.X , the following is how you build a client that uses a particular connectTimeoutMillis
and requestTimeoutMillis
.
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder = requestBuilder.setConnectTimeout(connectTimeoutMillis);
requestBuilder = requestBuilder.setConnectionRequestTimeout(requestTimeoutMillis);
clientBuilder.setDefaultRequestConfig(requestBuilder.build());
CloseableHttpClient httpClient = clientBuilder.build();
...
Btw, the javadocs for this code sucks. Try to figure out by hand how to use the config builder. Holy crap.
来源:https://stackoverflow.com/questions/20271017/connection-and-connection-request-timeout