Wednesday, August 6, 2014

Solution for ClientProtocolException Caused by CircularRedirectException.

Exception occurred while hitting URL:X(:((

 org.apache.http.client.ClientProtocolException   
 07-28 04:26:53.349: W/System.err(1276): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)  
 07-28 04:26:53.349: W/System.err(1276): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)  
 07-28 04:26:53.349: W/System.err(1276): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:78)  
 07-28 04:26:53.349: W/System.err(1276): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:102)  
 07-28 04:26:53.349: W/System.err(1276): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:58)  
 07-28 04:26:53.349: W/System.err(1276): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)  
 07-28 04:26:53.349: W/System.err(1276): at java.util.concurrent.FutureTask.run(FutureTask.java:234)  
 07-28 04:26:53.349: W/System.err(1276): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)  
 07-28 04:26:53.349: W/System.err(1276): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)  
 07-28 04:26:53.349: W/System.err(1276): at java.lang.Thread.run(Thread.java:841)  
 07-28 04:26:53.349: W/System.err(1276): Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'redirectURI'  
 07-28 04:26:53.353: W/System.err(1276): at org.apache.http.impl.client.DefaultRedirectHandler.getLocationURI(DefaultRedirectHandler.java:173)  
 07-28 04:26:53.353: W/System.err(1276): at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:923)  
 07-28 04:26:53.353: W/System.err(1276): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:475)  
 07-28 04:26:53.353: W/System.err(1276): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)  


Details / Information about the Exception: :-?


ProtocolException : Signals that an HTTP protocol violation has occurred. For example a malformed status line or headers, a missing message body, etc.
When RedirectHandler determines the location request is expected to be redirected to given the response from the target server and the current request execution context.

public static final String ALLOW_CIRCULAR_REDIRECTS = "http.protocol.allow-circular-redirects";
Defines whether circular redirects (redirects to the same location) should be allowed. The HTTP spec is not sufficiently clear whether circular redirects are permitted, therefore optionally they can be enabled

When the redirection and the parameter of "http.protocol.allow-circular-redirects" is false it works for the first time and from the second time it throws  CircularRedirectException("Circular redirect to '" + redirectURI + "'")

We may get doubt, why first time we aren't getting the exception and allowed but not the second time? *confused*

Let look getLocationURI method of DefaultRedirectHandler.java class, we can found the code snippet as follows.


 if (redirectLocations.contains(redirectURI)) {  
      throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'");  
 } else {  
      redirectLocations.add(redirectURI);  
 }  

When first time we hit, the redirectLocations object is null and it will initializes
 RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS);  
 if (redirectLocations == null) {  
      redirectLocations = new RedirectLocations();  
      context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);  
 }  

So, the redirectURI wont be available in the redirectLocations object. When we hit the second time, the redirectURI is existing in the redirectLocations object. So, the apache throwing CircularRedirectException.

Solutions: :-bd

Here we have 2 solutions, either one of them we can use.
1. getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);

2. extend DefaultRedirectHandler and modify getLocationURI method.




Thanks for reading :) 
Whether this post is helpful?

Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments / feedback's are welcome. :-h

No comments :

Post a Comment