HttpClient: Disable automatic redirect in HttpGet

Categories: Java; Tagged with: ; @ March 31st, 2014 21:20

Requirement:  disable auto redirection in HttpClient. e.g. for test purpose.

 

// 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);

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.