Java-Apache-HttpClient 发表于 2019-06-23 更新于 2024-03-18 分类于 Java , JMH 阅读次数: Valine: 阅读更多 1 Maven依赖 12345<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version></dependency> 2 重试机制 123456789private 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 超时设置 1234567HttpRequestBase request = new HttpGet();RequestConfig config = RequestConfig.custom() .setConnectTimeout(3000).setConnectionRequestTimeout(1000) .setSocketTimeout(5000).build();request.setConfig(config); 4 参考 HttpClient超时设置详解