Skip to content

Instantly share code, notes, and snippets.

@gary-liguoliang
Last active March 23, 2016 06:00
Show Gist options
  • Save gary-liguoliang/9481416 to your computer and use it in GitHub Desktop.
Save gary-liguoliang/9481416 to your computer and use it in GitHub Desktop.
Disable automatic redirect in HttpGet using HttpClient
// HttpClient Version: 4.3.3
RequestConfig requestConfig = RequestConfig.custom().setRedirectsEnabled(false).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = httpClient.execute(httpGet);
System.out.println("Status Code: " + response.getStatusLine().getStatusCode());
// Older version: http://stackoverflow.com/questions/1519392/how-to-prevent-apache-http-client-from-following-a-redirect
// HTTP parameters stores header etc.
HttpParams params = new BasicHttpParams();
params.setParameter("http.protocol.handle-redirects",false);
httpget.setParams(params);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment