Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling HttpClient Redirects

I'm POSTing some data to a server that is answering a 302 Moved Temporarily.

I want HttpClient to follow the redirect and automatically GET the new location, as I believe it's the default behaviour of HttpClient. However, I'm getting an exception and not following the redirect :(

Here's the relevant piece of code, any ideas will be appreciated:

HttpParams httpParams = new BasicHttpParams(); HttpClientParams.setRedirecting(httpParams, true); SchemeRegistry schemeRegistry = registerFactories(); ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);  HttpClient httpClient = new DefaultHttpClient(clientConnectionManager, httpParams) HttpPost postRequest = new HttpPost(url); postRequest.setHeader(HTTP.CONTENT_TYPE, contentType); postRequest.setHeader(ACCEPT, contentType);  if (requestBodyString != null) {     postRequest.setEntity(new StringEntity(requestBodyString)); }  return httpClient.execute(postRequest, responseHandler); 
like image 550
mgv Avatar asked Mar 02 '11 15:03

mgv


People also ask

How do I redirect in HttpClient?

Redirect policy is set through the Builder. followRedirects method. Implementation Note: When automatic redirection occurs, the request method of the redirected request may be modified depending on the specific 30X status code, as specified in RFC 7231.

How do I follow a redirect in Java?

Java Http Redirect ExampleIf a server is redirected from the original URL to another URL, the response code should be 301: Moved Permanently or 302: Temporary Redirect. And you can get the new redirected url by reading the “Location” header of the HTTP response header.

Does HttpClient need to be closed?

You do not need to explicitly close the HttpClient, however, (you may be doing this already but worth noting) you should ensure that connections are released after method execution. Edit: The ClientConnectionManager within the HttpClient is going to be responsible for maintaining the state of connections.

What is redirect strategy?

A 301 redirect strategy enables you to gather the valuable link authority from discontinued URLs and shift it to live, relevant ones, giving your customers the next best option when the page they requested no longer exists.


1 Answers

For HttpClient 4.3:

HttpClient instance = HttpClientBuilder.create()                      .setRedirectStrategy(new LaxRedirectStrategy()).build(); 

For HttpClient 4.2:

DefaultHttpClient client = new DefaultHttpClient(); client.setRedirectStrategy(new LaxRedirectStrategy()); 

For HttpClient < 4.2:

DefaultHttpClient client = new DefaultHttpClient(); client.setRedirectStrategy(new DefaultRedirectStrategy() {     /** Redirectable methods. */     private String[] REDIRECT_METHODS = new String[] {          HttpGet.METHOD_NAME, HttpPost.METHOD_NAME, HttpHead.METHOD_NAME      };      @Override     protected boolean isRedirectable(String method) {         for (String m : REDIRECT_METHODS) {             if (m.equalsIgnoreCase(method)) {                 return true;             }         }         return false;     } }); 
like image 161
RTF Avatar answered Sep 20 '22 02:09

RTF