Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Solution for ClientProtocolException Caused by CircularRedirectException

DZone's Guide to

Solution for ClientProtocolException Caused by CircularRedirectException

· Java Zone ·
Free Resource

Take 60 minutes to understand the Power of the Actor Model with "Designing Reactive Systems: The Role Of Actors In Distributed Architecture". Brought to you in partnership with Lightbend.

The following exception occurred while hitting the URL:

 org.apache.http.client.ClientProtocolException   
W/System.err(1276): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)  
W/System.err(1276): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)  
W/System.err(1276): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:78)  
W/System.err(1276): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:102)  
W/System.err(1276): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:58)  
W/System.err(1276): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)  
W/System.err(1276): at java.util.concurrent.FutureTask.run(FutureTask.java:234)  
W/System.err(1276): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)  
W/System.err(1276): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)  
W/System.err(1276): at java.lang.Thread.run(Thread.java:841)  
W/System.err(1276): Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'redirectURI'  
W/System.err(1276): at org.apache.http.impl.client.DefaultRedirectHandler.getLocationURI(DefaultRedirectHandler.java:173)  
W/System.err(1276): at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:923)  
W/System.err(1276): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:475)  
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 wonder why the first time we didn't get the exception and were allowed but not the second time? 


Let's look at 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: 

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.

Learn how the Actor model provides a simple but powerful way to design and implement reactive applications that can distribute work across clusters of cores and servers. Brought to you in partnership with Lightbend.

Topics:
cloud

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}