Solution for ClientProtocolException Caused by CircularRedirectException
Join the DZone community and get the full member experience.
Join For Freethe 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.
Opinions expressed by DZone contributors are their own.
Comments