43

我正在使用 apache http 客户端连接到远程服务器。远程服务器发送重定向,我想实现我的客户端没有自动遵循重定向,以便我可以提取propper标头并对目标执行我想要的任何操作。

我正在寻找一个简单的工作代码示例(复制粘贴)来停止自动重定向以下行为

我发现防止 HttpClient 4 跟随重定向,但似乎我太愚蠢了,无法用 HttpClient 4.0 (GA) 实现它

4

10 回答 10

49

感谢macbirdie的神奇之处在于:

params.setParameter("http.protocol.handle-redirects",false);

省略了导入,这是一个复制粘贴示例:

HttpClient httpclient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();

// HTTP parameters stores header etc.
HttpParams params = new BasicHttpParams();
params.setParameter("http.protocol.handle-redirects",false);

// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();

// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

// connect and receive 
HttpGet httpget = new HttpGet("http://localhost/web/redirect");
httpget.setParams(params);
response = httpclient.execute(httpget, localContext);

// obtain redirect target
Header locationHeader = response.getFirstHeader("location");
if (locationHeader != null) {
    redirectLocation = locationHeader.getValue();
  System.out.println("loaction: " + redirectLocation);
} else {
  // The response is invalid and did not provide the new location for
  // the resource.  Report an error or possibly handle the response
  // like a 404 Not Found error.
}
于 2009-10-05T11:20:04.910 回答
27

使用 HttpClient 4.3 和 Fluent:

final String url = "http://...";
final HttpClient client = HttpClientBuilder.create()
    .disableRedirectHandling()
    .build();
final Executor executor = Executor.newInstance(client);
final HttpResponse response = executor.execute(Request.Get(url))
    .returnResponse();
于 2014-02-04T18:03:54.093 回答
23

这对我有用:

HttpGet httpGet = new HttpGet("www.google.com");
HttpParams params = httpGet.getParams();
params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
httpGet.setParams(params);
于 2011-11-09T12:52:56.950 回答
16

默认HttpClient实现的可配置性非常有限,但您可以使用 HttpClient 的布尔参数来控制重定向处理http.protocol.handle-redirects

请参阅文档以供参考。

于 2009-10-05T11:02:00.797 回答
12

而不是直接使用该属性,您可以使用:

final HttpParams params = new BasicHttpParams();
HttpClientParams.setRedirecting(params, false);
于 2009-11-15T05:29:25.717 回答
2

HttpClient 4.3 之前

在旧版本的 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:/testabc.com");
    httpGet.setParams(params);
    CloseableHttpResponse response = instance.execute(httpGet);

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

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

HttpClientParams.setRedirecting(params, false);

另请注意,在禁用跟随重定向的情况下,我们现在可以检查 Http 响应状态代码是否确实是 301 Moved Permanently - 应该是。

HttpClient 4.3 之后

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://testabc.com"));

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

请注意,新 API 使用此重定向行为配置整个客户端 - 而不仅仅是单个请求。参考:http ://www.baeldung.com/httpclient-stop-follow-redirect

于 2017-03-26T06:27:17.850 回答
1

这对我有用 CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build()

于 2018-03-11T16:43:10.033 回答
0
GetMethod method = new GetMethod(url);
method.setFollowRedirects(false); 
于 2012-02-15T20:31:37.873 回答
0

为了避免自动重定向标头,必须首先将请求配置为不进行自动重定向。您可以通过调用HttPClientParams.setRedirection并将其设置为false. 代码片段如下所示:

HttpPost postURL = new HttpPost(resourceURL);
...
HttpClientParams.setRedirecting(postURL.getParams(), false);
于 2013-04-18T14:51:02.277 回答
0

而不是直接调用 HttpClientBuilder ,您可以使用

HttpClients.custom().disableRedirectHandling().build();
于 2016-05-10T08:15:05.157 回答