Apache HttpClient – Do Not Follow Redirects – Apache HttpClient – Do Not Follow Redirects

最后修改: 2013年 12月 29日

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

1. Overview

1.概述

In this article I will show how to configure the Apache HttpClient to stop following redirects.

在这篇文章中,我将展示如何配置Apache HttpClient以停止跟随重定向

By default, following the HTTP Spec, the HttpClient will automatically follow redirects.

默认情况下,遵循HTTP规范,HttpClient将自动遵循重定向

For some usecases, that may be perfectly fine, but there are certainly usecases where that’s not desired – and we’ll now look at how to change that default behavior and stop following redirects.

对于某些情况来说,这可能是完美的,但肯定有一些情况是不希望这样的–我们现在将看看如何改变这种默认行为,停止跟踪重定向

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教程

2. Do Not Follow Redirects

2.不要跟随重定向

2.1. Before HttpClient 4.3

2.1.在HttpClient 4.3之前

In older versions of the Http Client (before 4.3), we can configure what the client does with redirects as follows:

在旧版本的Http客户端中(4.3之前),我们可以按以下方式配置客户端对重定向的操作。

@Test
public void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() 
  throws ClientProtocolException, IOException {
    DefaultHttpClient instance = new DefaultHttpClient();

    HttpParams params = new BasicHttpParams();
    params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);
    // HttpClientParams.setRedirecting(params, false); // alternative

    HttpGet httpGet = new HttpGet("http://t.co/I5YYd9tddw");
    httpGet.setParams(params);
    CloseableHttpResponse response = instance.execute(httpGet);

    assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
}

Notice the alternative API that can be used to configure the redirect behavior without using setting the actual raw http.protocol.handle-redirects parameter:

请注意可用于配置重定向行为的替代API,而不使用设置实际的原始http.protocol.handle-redirects参数

HttpClientParams.setRedirecting(params, false);

Also notice that, with follow redirects disabled, we can now check that the Http Response status code is indeed 301 Moved Permanently – as it should be.

还注意到,在禁用跟随重定向后,我们现在可以检查Http响应状态代码确实是301 Moved Permanently–因为它应该是。

2.2. After HttpClient 4.3

2.2.在HttpClient 4.3之后

HttpClient 4.3 introduced a cleaner, more high level API to build and configure the client:

HttpClient 4.3引入了一个更干净、更高水平的API来构建和配置客户端。

@Test
public void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() 
  throws ClientProtocolException, IOException {
    HttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();
    HttpResponse response = instance.execute(new HttpGet("http://t.co/I5YYd9tddw"));

    assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
}

Note that the new API configures the entire client with this redirect behavior – not just the individual request.

请注意,新的API用这种重定向行为来配置整个客户端–而不仅仅是单个请求。

3. Conclusion

3.结论

This quick tutorial covered how to configure the Apache HttpClient – both pre 4.3 and post – to prevent it from following HTTP redirects automatically.

这个快速教程涵盖了如何配置Apache HttpClient–包括4.3之前和之后–以防止它自动跟随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的项目,所以它应该很容易导入并按原样运行。