HttpClient 301问题

HttpClient 301问题

问题

项目中使用到了apache的HttpClient 4.5版本来进行http操作。
最近在开发时和其他服务端使用post进行对接,有些接口报错,报错如下:

responseResult : HttpResponseProxy{HTTP/1.1 301 Moved Permanently

鉴于和运维沟通麻烦,修改域名不方便等问题,决定在自己这边做一下兼容,即返回301后自动请求新的url。

解决方案

自定义一个重定向策略,对返回值301进行处理

HttpClientBuilder.create().setRedirectStrategy(new CustomLaxRedirectStrategy()).build();

static class CustomLaxRedirectStrategy extends LaxRedirectStrategy {
        @Override
        public HttpUriRequest getRedirect(
                final HttpRequest request,
                final HttpResponse response,
                final HttpContext context) throws ProtocolException {
            final URI uri = getLocationURI(request, response, context);
            final String method = request.getRequestLine().getMethod();
            if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
                return new HttpHead(uri);
            } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
                return new HttpGet(uri);
            } else {
                final int status = response.getStatusLine().getStatusCode();
                //针对post请求301重定向问题
                if (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == HttpStatus.SC_MOVED_PERMANENTLY) {
                    return RequestBuilder.copy(request).setUri(uri).build();
                } else {
                    return new HttpGet(uri);
                }
            }
        }
    }

过程

当遇到问题,最简单的就是百度。发现都是推荐使用LaxRedirectStrategy策略来解决。于是,我就改了下:HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
然后重新向问题接口发起请求。但是返回依然是301。
于是,我对execute进行断点,执行顺序如下:
在这里插入图片描述
在这里该方法会判断是否进行重定向以及以何种方式进行重定向。

public HttpUriRequest getRedirect(
            final HttpRequest request,
            final HttpResponse response,
            final HttpContext context) throws ProtocolException {
        final URI uri = getLocationURI(request, response, context);
        final String method = request.getRequestLine().getMethod();
        if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
            return new HttpHead(uri);
        } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
            return new HttpGet(uri);
        } else {
        	//由于不是head,也不是get,是post方法,因此进入了这里,但这里只对307进行了处理,其他都返回get请求
            final int status = response.getStatusLine().getStatusCode();
            if (status == HttpStatus.SC_TEMPORARY_REDIRECT) {
                return RequestBuilder.copy(request).setUri(uri).build();
            } else {
                return new HttpGet(uri);
            }
        }
    }

于是我自定义了重定向策略,继承自LaxRedirectStrategy,当再次请求时,就进入了自定义的getRedirect方法。此时判断status,由于是301,于是复制请求,并进行重定向。

if (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == HttpStatus.SC_MOVED_PERMANENTLY) {
                    return RequestBuilder.copy(request).setUri(uri).build();
                }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值