HttpClient 4 的Post 重定向

简介

这篇文章将教大家怎样配置Apache HttpClient 4 自动跟随Post请求的重定向。
默认的情况下,只有GET请求是自动遵循重定向的。如果一个POST请求返回的状态是HTTP 301或HTTP 302的话, 是不会自动重定向的。

这里是[web link="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3"]HTTP RFC 2616[/weblink]的具体说明:

If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

我们下面就尝试下POST的301跳转: 先来看下默认的POST请求

@Test
public void givenPostRequest_whenConsumingUrlWhichRedirects_thenNotRedirected() 
  throws ClientProtocolException, IOException {
    HttpClient instance = HttpClientBuilder.create().build();
    HttpResponse response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw"));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
}

和你看到的一样,返回的是301状态码。并没能自动重定向

HTTP POST 重定向

**2.1 4.3以及更高的httplient版本 在HttpClient 4.3 或者更高的版本 的API介绍了创建和配置client:

@Test
public void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() 
  throws ClientProtocolException, IOException {
    HttpClient instance = 
      HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
    HttpResponse response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw"));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
HttpClientBuilder 现在比以前更容易使用。允许client 完整的配置。比起以前,更加具有可读性;

2.2 httplient 4.2 在4.2这个版本中,我们可以直接在client上配置重定向规则:

@SuppressWarnings("deprecation")
@Test
public void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() 
  throws ClientProtocolException, IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new LaxRedirectStrategy());</p>
	HttpResponse response = client.execute(new HttpPost("http://t.co/I5YYd9tddw"));
	assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}

使用 new LaxRedirectStrategy() 让HTTP 限制放宽。能在post的时候自动跟随从定向-返回200的请求状态

2.3 HttpClient 4.2以前的版本
在HttpClient 4.2以前的版本中,LaxRedirectStrategy 这个类是不存在的,所以我们需要自己实现一下:

@Test
public void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() 
  throws ClientProtocolException, IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new DefaultRedirectStrategy() {
        /*<em> Redirectable methods. </em>/
        private String[] REDIRECT_METHODS = new String[] { 
            HttpGet.METHOD_NAME, HttpPost.METHOD_NAME, HttpHead.METHOD_NAME 
        };
     @Override
    protected boolean isRedirectable(String method) {
        for (String m : REDIRECT_METHODS) {
            if (m.equalsIgnoreCase(method)) {
                return true;
            }
        }
        return false;
    }
});

HttpResponse response = client.execute(new HttpPost("http://t.co/I5YYd9tddw"));
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}

结束语

这篇文章说明了怎样在Apache HttpClient 4 各个不同的版本中配置POST的redirects重定向请求。有问题欢迎留言一起讨论解决

转载于:https://my.oschina.net/u/265943/blog/292878

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值