Java Apache http客户端使用重定向重试
我正在使用ApacheHTTP客户端,希望下载文件。外部服务具有以下功能:Java Apache http客户端使用重定向重试,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,我正在使用ApacheHTTP客户端,希望下载文件。外部服务具有以下功能: 要下载我发送GET请求的文件 这会重定向到URL,在那里我可以开始下载文件 问题是,文件可以定位(复制)在一堆URL上,并且不能访问所有URL(有些主机甚至不能解析为IP) 我已将重试处理程序设置为自定义http客户端: HttpClients.custom() .setRetryHandler(new HttpRequestRetryHandler(4)) .setRed
HttpClients.custom()
.setRetryHandler(new HttpRequestRetryHandler(4))
.setRedirectStrategy(new LaxRedirectStrategy())
...
.build();
但问题是重试只在重定向后应用。所以,若我无法访问重定向的URL,那个么httpClient将对其执行4个请求(ofc所有请求都将失败)。
如何对初始URL进行重试?在深入挖掘源代码后,现在似乎不可能(版本4.5.13)。自定义客户端生成器:。在构建时,它首先将execChain包装到RetryExec中,然后再包装到RedirectExec中。
class HttpRequestRetryHandler extends DefaultHttpRequestRetryHandler {
public HttpRequestRetryHandler(int retryCount) {
super(retryCount, true, Arrays.asList(InterruptedIOException.class, SSLException.class));
}
@Override
protected boolean handleAsIdempotent(HttpRequest request) {
String method = request.getRequestLine().getMethod().toUpperCase(Locale.ROOT);
return "GET".equals(method);
}
}