HttpClient 4 的Post 重定向

简介

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

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

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.web

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

@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状态码。并没能自动重定向ide

HTTP POST 重定向

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

@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上配置重定向规则: ui

@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的请求状态this

2.3 HttpClient 4.2之前的版本
在HttpClient 4.2之前的版本中,LaxRedirectStrategy 这个类是不存在的,因此咱们须要本身实现一下:
code

@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重定向请求。有问题欢迎留言一块儿讨论解决htm

相关文章
相关标签/搜索