当前位置: 首页 > 面试题库 >

强制重试特定的HTTP状态代码

胥英奕
2023-03-14
问题内容

在处理特定网站时,有时会收到状态码为403的http响应。在这种情况下,我想重新执行该请求(因为在我的特定情况下,该服务器实际上超载时会抛出403)。我曾尝试将a
ResponseHandler和a
一起使用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 int executionCount, final HttpContext context) {
                int statusCode = response.getStatusLine().getStatusCode();
                return statusCode == 403 && executionCount < 5;
            }

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


 类似资料:
  • 我考虑在项目中使用fluent-http。 所以我尝试注入: 返回正确的字符串,但似乎没有使用状态代码。在这两种情况下,响应都有一个代码HTTP200。 注意:我发现一些状态代码是预先实现的: null

  • 问题内容: 我想记录301s和302s,但是找不到在Client.Do,Get,doFollowingRedirects,CheckRedirect中读取响应状态代码的方法。我是否必须自己实施重定向才能实现这一目标? 问题答案: 该类型允许您指定自定义传输方式,这应允许您执行后续操作。应该执行以下操作: (如果仅支持链接到默认传输,则可以稍微简化一下)。 然后,您可以使用此传输方式创建客户端,并记

  • 问题内容: 我现在将脚本更改为jquery。这是我的旧javascript: 我现在想将其更改为jquery-ajax: 像在旧脚本中一样,如何测试请求返回的状态码(和responseText)? 问题答案: 试试这个: 要么:

  • 但是我不明白如何使用注释方法来实现这一点。 有人能帮忙吗?谢了。

  • 问题内容: 我有一个应用程序在heroku上运行,并在带有https的节点上运行express。如何确定协议,以在Heroku上使用nodejs强制重定向到https? 我的应用程序只是一个简单的http服务器,它(尚未)意识到heroku正在向其发送https请求: 问题答案: 答案是使用Heroku传递给它的代理thingamabob时使用的“ x-forwarded-proto”标头。(旁注

  • 我还用测试了这一点...现在它甚至不再像以前那样工作了!! 所有这些都是通过诸如、和等类/接口来处理的(都在中设置)。显然,有一个重定向到URL的操作,将原始请求/和/或响应包装到其他内容中,例如,我可以将请求调试为符合以下内容:SecurityContextHolderAwareRequestWrapper[FirewalledRequest[org.apache.catalina.core.A