如何防止apache http客户端遵循redirect
我使用apache http客户端连接到远程服务器。 远程服务器发送一个redirect,我想实现我的客户端不是自动redirect,以便我可以提取propper头,并做任何我想要的目标。
我正在寻找一个简单的工作代码示例 (复制粘贴) ,停止自动redirect以下行为 。
我发现阻止HttpClient 4遵循redirect ,但似乎我太愚蠢与HttpClient 4.0(GA)实施它,
默认的HttpClient
实现在可configuration性上相当有限,但是可以使用HttpClient的布尔参数http.protocol.handle-redirects
来控制redirect处理。
请参阅文档以供参考。
感谢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. }
这对我工作:
HttpGet httpGet = new HttpGet("www.google.com"); HttpParams params = httpGet.getParams(); params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE); httpGet.setParams(params);
使用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();
而不是直接使用属性,你可以使用:
final HttpParams params = new BasicHttpParams(); HttpClientParams.setRedirecting(params, false);
GetMethod method = new GetMethod(url); method.setFollowRedirects(false);
要避免自动redirect头,首先必须configuration请求不自动redirect。 你可以通过调用HttPClientParams.setRedirection
设置它为false
。 代码片段如下所示:
HttpPost postURL = new HttpPost(resourceURL); ... HttpClientParams.setRedirecting(postURL.getParams(), false);
在HttpClient 4.3之前
在较旧版本的Http客户端(4.3之前)中,我们可以configuration客户端redirect的function,如下所示:
@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来configurationredirect行为, 而无需使用设置实际的原始http.protocol.handle-redirects参数:
HttpClientParams.setRedirecting(params, false);
另外请注意,在禁用后续redirect的情况下,我们现在可以检查Http Response状态代码是否确实是301永久移动 – 应该是这样。
HttpClient 4.3后
HttpClient 4.3引入了一个更清洁, 更高层次的API来构build和configuration客户端:
@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使用此redirect行为来configuration整个客户端,而不仅仅是个别请求。 参考: http : //www.baeldung.com/httpclient-stop-follow-redirect
而不是直接调用HttpClientBuilder,你可以使用
HttpClients.custom().disableRedirectHandling().build();
对于HttURLConnection,他们暴露了一个API。 因此,如果它检测到任何自动redirect,那么它将返回302响应代码,而不是200.所以,通过跟踪响应代码,我们可以轻松跟踪它是否是redirectUrl。
HttpURLConnection httpURLConnection = (HttpURLConnection) (new URL("http://www.redirecturl.com").openConnection()); httpURLConnection.setInstanceFollowRedirects(false); httpURLConnection.connect(); int responseCode = httpURLConnection.getResponseCode(); //302 in case of redirection.