Why does HttpClient throw a SocketTimeOutException on POST

I have code similar to the following:

try {
    HttpPost post = new HttpPost(httpsUrl);
    setHeaders(post);

    HttpEntity entity = new StringEntity(request, "UTF-8");

    post.setEntity(entity);

    HttpResponse response = httpclient.execute(post);
    String result = EntityReader.readContent(response.getEntity());
    checkAnswer(result);
    return result;

} catch (Exception e) {
    throw new ZapException("Error executing the http post request: "+e.getMessage(), e);
}

It sends content requestto the server via POST using an httpclient instance that may already have been used before (it has constant connections since we send quite some requests to the same server ...).

Sometimes this is interrupted with the help of SocketTimeoutExceptionthe message "Timeout". It is not clear to us why this only fails in some cases, when in most cases this is not so. What gives?

+3
source share
1 answer

, Apache Commons HttpClient (org.apache.commons.httpclient.HttpClient).

, t SocketTimeoutException , , HttpClient , , HttpClient. - -

HttpConnectionParams params = httpclient.getHttpConnectionManager().getParams();
params.setConnectionTimeout(20000);
params.setSoTimeout(15000);

, - -, -, SocketTimeoutException - , .

+1

Source: https://habr.com/ru/post/1752813/


All Articles