Apache HttpClient – Follow Redirects for POST – Apache HttpClient – Follow Redirects for POST

最后修改: 2013年 12月 29日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

This quick tutorial will show how to configure the Apache HttpClient to automatically follow redirects for POST requests.

这个快速教程将展示如何配置Apache HttpClient以自动跟踪POST请求的重定向。

If you want to dig deeper and learn other cool things you can do with the HttpClient – head on over to the main HttpClient tutorial.

如果你想更深入地了解你可以用HttpClient做的其他很酷的事情–请到主要HttpClient教程

By default, only GET requests resulting in a redirect are automatically followed. If a POST requests is answered with either HTTP 301 Moved Permanently or with 302 Foundthe redirect is not automatically followed.

默认情况下,只有导致重定向的GET请求会被自动跟踪。如果一个POST请求被回复为HTTP 301 Moved Permanently302 Found重定向不会被自动跟踪

This is specified by the HTTP RFC 2616:

这是由HTTP RFC 2616指定的。

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.

如果收到301状态代码是对GET或HEAD以外的请求的响应,用户代理不得自动重定向该请求,除非它能被用户确认,因为这可能改变发出请求的条件。

There are of course usecases where we need to change that behavior and relax the strict HTTP specification.

当然,在有些情况下,我们需要改变这种行为,放松严格的HTTP规范。

First, let’s check the default behavior:

首先,让我们检查一下默认行为。

@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));
}

As you can see, the redirect is not followed by default, and we get back the 301 Status Code.

正如你所看到的,默认情况下没有遵循重定向,我们得到的是301状态代码

2. Redirecting on HTTP POST

2.HTTP POST的重定向

2.1. For HttpClient 4.3 and After

2.1.适用于HttpClient 4.3及以后版本

In HttpClient 4.3, a higher level API has been introduced for both creation and configuration of the client:

在HttpClient 4.3中,为客户端的创建和配置引入了一个更高层次的API。

@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));
}

Notice the HttpClientBuilder is now the starting point of a fluent API which allows full configuration of the client in a more readable way than before.

注意HttpClientBuilder现在是一个流畅的API的起点,它允许以比以前更可读的方式对客户端进行全面配置。

2.2. For HttpClient 4.2

2.2.用于HttpClient 4.2

In the previous version of HttpClient (4.2) we can configure the redirect strategy directly on the client:

在HttpClient(4.2)的前一个版本中,我们可以直接在客户端配置重定向策略。

@SuppressWarnings("deprecation")
@Test
public void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() 
  throws ClientProtocolException, IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new LaxRedirectStrategy());

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

Notice that now, with the new LaxRedirectStrategy, the HTTP Restrictions are relaxed and the redirect is followed over POST as well – leading to a 200 OK status code.

注意,现在有了新的LaxRedirectStrategy,HTTP限制被放宽了,重定向也是通过POST进行的–导致200 OK状态代码。

2.3. Pre HttpClient 4.2

2.3.预先的HttpClient 4.2

Before HttpClient 4.2, the LaxRedirectStrategy class didn’t exist, so we need to roll our own:

在HttpClient 4.2之前,LaxRedirectStrategy类并不存在,所以我们需要推出自己的。

@Test
public void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() 
  throws ClientProtocolException, IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new DefaultRedirectStrategy() {
        /** Redirectable methods. */
        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));
}

3. Conclusion

3.结论

This quick guide illustrated how to configure any version of the Apache HttpClient to follow redirects for HTTP POST requests as well – relaxing the strict HTTP standard.

这个快速指南说明了如何配置任何版本的Apache HttpClient,使其对HTTP POST请求也能遵循重定向–放松严格的HTTP标准。

The implementation of all these examples and code snippets can be found in my github project – this is an Eclipse based project, so it should be easy to import and run as it is.

所有这些例子和代码片段的实现可以在我的github项目中找到 – 这是一个基于Eclipse的项目,所以它应该很容易导入并按原样运行。