0%

Java-Apache-HttpClient

阅读更多

1 Maven依赖

1
2
3
4
5
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>

2 重试机制

1
2
3
4
5
6
7
8
9
private static final HttpClient HTTP_CLIENT = HttpClientBuilder.create()
.setRetryHandler((IOException exception, int executionCount, HttpContext context) -> {
if (executionCount > 3) {
return false;
}
return exception instanceof NoHttpResponseException || exception instanceof ConnectTimeoutException;
})
.build();

3 超时设置

1
2
3
4
5
6
7
HttpRequestBase request = new HttpGet();

RequestConfig config = RequestConfig.custom()
.setConnectTimeout(3000).setConnectionRequestTimeout(1000)
.setSocketTimeout(5000).build();

request.setConfig(config);

4 参考