如何正确处理HTTP请求错误?
How to correctly handling HTTP request errors?
我有一个简单的 http 客户端,它将每个请求传递给 ExecutorService 并延迟应用它们。
protected static final int RETRY_ATTEMPTS = 5;
private static final int GROUP_REQUEST_DELAY_MS = 55;
private static final ScheduledExecutorService REQUEST_FROM_USER_EXECUTOR =
Executors.newSingleThreadScheduledExecutor();
public RequestResponse post(String url) throws IOException {
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = call(httpPost);
return new RequestResponse(
httpResponse.getStatusLine().getStatusCode(),
EntityUtils.toString(httpResponse.getEntity()),
headers(httpResponse.getAllHeaders())
);
}
private HttpResponse call(HttpRequestBase request) throws IOException {
int attempts = 0;
HttpResponse httpResponse = null;
SocketException socketException = null;
do {
try {
httpResponse = client.execute(request);
} catch(SocketException e) {
socketException = e;
}
if(httpResponse != null)
break;
attempts++;
log.debug("Attempt: {}, SocEx: {}", attempts, socketException != null);
}while(attempts < RETRY_ATTEMPTS);
if(httpResponse == null)
// TODO
if(socketException != null) {
log.error("Network problem");
logRequest(request, httpResponse);
throw socketException;
}
return httpResponse;
}
public synchronized Future<RequestResponse> sendAsGroup(String url) {
return REQUEST_FROM_GROUP_EXECUTOR.schedule(() -> post(url), GROUP_REQUEST_DELAY_MS, TimeUnit.MILLISECONDS);
}
有时服务器会抛出 HTTP 504 左右的错误。我想处理这个错误并重新提交这个请求。如何在不超过服务器请求限制的情况下正确执行此操作?
您应该使用 HttpRequestRetryHandler
从传输层 (TCP) 错误中恢复,并使用 ServiceUnavailableRetryStrategy
在出现协议层 (HTTP) 错误时重试请求执行。
我有一个简单的 http 客户端,它将每个请求传递给 ExecutorService 并延迟应用它们。
protected static final int RETRY_ATTEMPTS = 5;
private static final int GROUP_REQUEST_DELAY_MS = 55;
private static final ScheduledExecutorService REQUEST_FROM_USER_EXECUTOR =
Executors.newSingleThreadScheduledExecutor();
public RequestResponse post(String url) throws IOException {
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = call(httpPost);
return new RequestResponse(
httpResponse.getStatusLine().getStatusCode(),
EntityUtils.toString(httpResponse.getEntity()),
headers(httpResponse.getAllHeaders())
);
}
private HttpResponse call(HttpRequestBase request) throws IOException {
int attempts = 0;
HttpResponse httpResponse = null;
SocketException socketException = null;
do {
try {
httpResponse = client.execute(request);
} catch(SocketException e) {
socketException = e;
}
if(httpResponse != null)
break;
attempts++;
log.debug("Attempt: {}, SocEx: {}", attempts, socketException != null);
}while(attempts < RETRY_ATTEMPTS);
if(httpResponse == null)
// TODO
if(socketException != null) {
log.error("Network problem");
logRequest(request, httpResponse);
throw socketException;
}
return httpResponse;
}
public synchronized Future<RequestResponse> sendAsGroup(String url) {
return REQUEST_FROM_GROUP_EXECUTOR.schedule(() -> post(url), GROUP_REQUEST_DELAY_MS, TimeUnit.MILLISECONDS);
}
有时服务器会抛出 HTTP 504 左右的错误。我想处理这个错误并重新提交这个请求。如何在不超过服务器请求限制的情况下正确执行此操作?
您应该使用 HttpRequestRetryHandler
从传输层 (TCP) 错误中恢复,并使用 ServiceUnavailableRetryStrategy
在出现协议层 (HTTP) 错误时重试请求执行。