Java : HttpClient 4.1.2 : ConnectionTimeout, SocketTimeout values set are not effective

你离开我真会死。 提交于 2019-12-04 06:18:24
Vlad

The HttpConnectionParams need to be passed to a connection manager (see this question). When using the DefaultHttpClient you can set these parameters like this:

    httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
    httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);

See the documentation also!

You can try this (works with apache http-client 4.5.2):

int DEFAULT_TIMEOUT = 5000;
RequestConfig requestConfig = RequestConfig.custom()
   .setConnectTimeout(DEFAULT_TIMEOUT)
   .setConnectionRequestTimeout(DEFAULT_TIMEOUT)
   .setSocketTimeout(DEFAULT_TIMEOUT)
   .build();
CloseableHttpClient httpClient = HttpClients.custom()
   .setDefaultRequestConfig(requestConfig)
   .build();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!