加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 强制重试特定的http状态代码

发布时间:2020-12-14 05:27:59 所属栏目:Java 来源:网络整理
导读:处理特定网站时,有时我会收到状态代码为403的http响应.我想在这种情况下重新执行请求(因为,在我的特定情况下,此服务器在实际超载时会抛出403).我尝试将ResponseHandler与StandardHttpRequestRetryHandler一起使用,但它没有按照我希望的方式工作;我期望在Resp
处理特定网站时,有时我会收到状态代码为403的http响应.我想在这种情况下重新执行请求(因为,在我的特定情况下,此服务器在实际超载时会抛出403).我尝试将ResponseHandler与StandardHttpRequestRetryHandler一起使用,但它没有按照我希望的方式工作;我期望在ResponseHandler中抛出异常会触发StandardHttpRequestRetryHandler,但似乎并非如此.如何实现所需的功能?

这是一个示例代码,说明了我的情况:

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;


public class Main {

  public static void main(String[] args) throws Exception {

    // a response handler that throws an exception if status is not 200
    ResponseHandler<String> responseHandler = new ResponseHandler<String> () {

      @Override
      public String handleResponse(HttpResponse response) throws
        ClientProtocolException,IOException
      {
        System.out.println("-> Handling response");

        if (response.getStatusLine().getStatusCode() != 200){
          // I expected this to trigger the retryHandler 
          throw new ClientProtocolException("Status code not supported");
        }
        return EntityUtils.toString(response.getEntity());
      }

    };

    StandardHttpRequestRetryHandler retryHandler = 
        new StandardHttpRequestRetryHandler(5,true)
    {
      @Override
      public boolean retryRequest(
          final IOException exception,final int executionCount,final HttpContext context)
      {
        System.out.println("-> Retrying request");
        return super.retryRequest(exception,executionCount,context);
      }
    };

    // my client with my retry handler
    HttpClient client = HttpClients
        .custom()
        .setRetryHandler(retryHandler)
        .build();

    // my request
    HttpUriRequest request = RequestBuilder
        .create("GET")
        .setUri("http://httpstat.us/403")         //always returns 403
        .build();

    String contents = client.execute(request,responseHandler);

    System.out.println(contents);
  }


}

解决方法

尝试使用自定义ServiceUnavailableRetryStrategy
CloseableHttpClient client = HttpClients.custom()
        .setServiceUnavailableRetryStrategy(new ServiceUnavailableRetryStrategy() {
            @Override
            public boolean retryRequest(
                    final HttpResponse response,final HttpContext context) {
                int statusCode = response.getStatusLine().getStatusCode();
                return statusCode == 403 && executionCount < 5;
            }

            @Override
            public long getRetryInterval() {
                return 0;
            }
        })
        .build();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读