Over a million developers have joined DZone.

Solution for ClientProtocolException Caused by CircularRedirectException

DZone's Guide to

Solution for ClientProtocolException Caused by CircularRedirectException

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

The following exception occurred while hitting the URL:

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 {  

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.


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.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}