迁移到 Apache HttpClient 5.x

问题描述 投票:0回答:1

如何迁移到低于 Apache httpcomponents-client 5.x 的 setRetryStrategy

HttpClientBuilder cb = HttpClients.custom();
cb.setRetryHandler(new DefaultHttpRequestRetryHandler(X, true));

最新的DefaultHttpRequestStrategy中没有requestSentRetryEnabled的参数

迁移到 httpcomponents-client 5.x

httpclient apache-httpcomponents apache-httpclient-5.x
1个回答
0
投票

requestSentRetryEnabled不是必需的,如果您将RetryHandler设置为DefaultHttpRequestStrategy,如果错误是下面的503示例代码,它将重试

public static void main(String[] args) throws IOException {
    final ClassicHttpRequest httpGet = ClassicRequestBuilder.get("https://httpbin.org/status/503")
            .build();

    CloseableHttpClient httpClient = HttpClientBuilder
            .create()
            .setRetryStrategy(new DefaultHttpRequestRetryStrategy(3, TimeValue.ofSeconds(3)))
            .build();

    httpClient.execute(httpGet, response -> {
        System.out.println(response.getCode() + " " + response.getReasonPhrase());
        return null;
    });
}

如果您想查看更多自定义策略,请重试所有检查此处

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.