我正在使用apache http客户端连接到远程服务器。远程服务器发送重定向,我想实现我的客户端没有自动跟踪重定向,以便我可以提取propper标头并做任何我想要的目标。
我正在寻找简单的工作代码示例(复制粘贴)停止自动重定向跟随行为。
我找到了Preventing HttpClient 4 from following redirect,但是用HttpClient 4.0(GA)实现它似乎太愚蠢了
答案 0 :(得分: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.
}
答案 1 :(得分:24)
这对我有用:
HttpGet httpGet = new HttpGet("www.google.com");
HttpParams params = httpGet.getParams();
params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
httpGet.setParams(params);
答案 2 :(得分:24)
使用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();
答案 3 :(得分:17)
默认HttpClient
实现在可配置性方面非常有限,但您可以使用HttpClient的布尔参数http.protocol.handle-redirects
来控制重定向处理。
请参阅docs以供参考。
答案 4 :(得分:13)
不是直接使用该属性,而是可以使用:
final HttpParams params = new BasicHttpParams();
HttpClientParams.setRedirecting(params, false);
答案 5 :(得分:1)
GetMethod method = new GetMethod(url);
method.setFollowRedirects(false);
答案 6 :(得分:1)
要避免自动重定向标头,必须首先将请求配置为不执行自动重定向。您可以致电HttPClientParams.setRedirection
并将其设为false
来执行此操作。代码段如下所示:
HttpPost postURL = new HttpPost(resourceURL);
...
HttpClientParams.setRedirecting(postURL.getParams(), false);
答案 7 :(得分:1)
在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已永久移动 - 应该是这样。
在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
答案 8 :(得分:0)
而不是直接调用HttpClientBuilder,可以使用
HttpClients.custom().disableRedirectHandling().build();
答案 9 :(得分:0)
对于HttURLConnection,他们暴露了一个api。因此,如果它检测到任何自动重定向,那么它将返回302响应代码而不是200.因此,通过跟踪响应代码,我们可以轻松跟踪它是否是重定向Url。
HttpURLConnection httpURLConnection = (HttpURLConnection) (new URL("http://www.redirecturl.com").openConnection());
httpURLConnection.setInstanceFollowRedirects(false);
httpURLConnection.connect();
int responseCode = httpURLConnection.getResponseCode(); //302 in case of redirection.
答案 10 :(得分:0)