Apache HttpClient返回未找到资源URL的SocketTimeout异常



我正在尝试使用apache http客户端下载图像。如果一个url没有找到资源(404),apache httpclient会间歇性地返回404或sockettimeout异常。

谁能帮助我理解为什么相同的url返回两个不同的响应。

下面是我的配置值

RequestConfig config = RequestConfig.custom()
.setSocketTimeout(10000)
.setConnectTimeout(5000)
.setRedirectsEnabled(true)
.setMaxRedirects(3)
.setStaleConnectionCheckEnabled(false)
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
.build();
Future<HttpResponse> httpResponseFuture = asyncCloseableHttpClient.execute(httpUriRequest, null);
try {
return httpResponseFuture.get();
} catch (ExecutionException e) {
Throwable cause = e.getCause() != null ? e.getCause() : e;

if (cause instanceof ConnectException) {
throw new DownloadConnectionException("ConnectionException " + cause, IMAGE_DOWNLOAD_FAILED, cause);
}
if (cause instanceof SocketTimeoutException) {

throw new DownloadTimeoutException(DOWNLOAD_TIMEOUT_EXCEPTION);
}
if (cause instanceof ConnectionClosedException) {

throw new DownloadConnectionClosedException("ConnectionClosedException " + cause, IMAGE_DOWNLOAD_FAILED, cause);
}
if(cause instanceof UnsupportedCharsetException) {

throw new BadRequestException("Image download failed with UnsupportedCharsetException " + cause,
INVALID_CONTENT_TYPE_EXCEPTION, cause);
}
if (cause instanceof CircuitBreakerOpenException)
throw cause;
throw new BadRequestException("Image download failed: " + e.getMessage(),
IMAGE_DOWNLOAD_FAILED, cause);
}

我试图访问的URL:https://decalpitstop.com/ca/citruschevron-sagnpro12.jpg

因为有时会超时,有时不会。代码中没有任何东西会导致这种行为,因此它一定是基础设施问题。可能涉及到负载平衡器,或者流量很大。它也看起来像你正在使用代理,所以它可能是代理造成的问题。此外,它可能会在一些重定向失败(您正在遵循最大3)。有很多可能性。

打开apache调试日志,查看正在进行的实际传输。这应该可以让您了解发生了什么

最新更新