我正在使用 apache http 客户端连接到远程服务器。远程服务器发送重定向,我想实现我的客户端没有自动遵循重定向,以便我可以提取propper标头并对目标执行我想要的任何操作。
我正在寻找一个简单的工作代码示例(复制粘贴)来停止自动重定向以下行为。
我发现防止 HttpClient 4 跟随重定向,但似乎我太愚蠢了,无法用 HttpClient 4.0 (GA) 实现它
我正在使用 apache http 客户端连接到远程服务器。远程服务器发送重定向,我想实现我的客户端没有自动遵循重定向,以便我可以提取propper标头并对目标执行我想要的任何操作。
我正在寻找一个简单的工作代码示例(复制粘贴)来停止自动重定向以下行为。
我发现防止 HttpClient 4 跟随重定向,但似乎我太愚蠢了,无法用 HttpClient 4.0 (GA) 实现它
感谢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.
}
使用 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();
这对我有用:
HttpGet httpGet = new HttpGet("www.google.com");
HttpParams params = httpGet.getParams();
params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
httpGet.setParams(params);
默认HttpClient
实现的可配置性非常有限,但您可以使用 HttpClient 的布尔参数来控制重定向处理http.protocol.handle-redirects
。
请参阅文档以供参考。
而不是直接使用该属性,您可以使用:
final HttpParams params = new BasicHttpParams();
HttpClientParams.setRedirecting(params, false);
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
这对我有用
CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build()
GetMethod method = new GetMethod(url);
method.setFollowRedirects(false);
为了避免自动重定向标头,必须首先将请求配置为不进行自动重定向。您可以通过调用HttPClientParams.setRedirection
并将其设置为false
. 代码片段如下所示:
HttpPost postURL = new HttpPost(resourceURL);
...
HttpClientParams.setRedirecting(postURL.getParams(), false);
而不是直接调用 HttpClientBuilder ,您可以使用
HttpClients.custom().disableRedirectHandling().build();