DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Implementing WOPI Protocol For Office Integration
  • Writing DTOs With Java8, Lombok, and Java14+
  • Efficient String Formatting With Python f-Strings
  • Java String: A Complete Guide With Examples

Trending

  • How Developers Are Driving Supply Chain Innovation With Modern Tech
  • Indexed Views in SQL Server: A Production DBA's Complete Guide
  • 10 Predictions Shaping the Future of Web Data Extraction Services
  • Understanding N-Gram Language Models and Perplexity
  1. DZone
  2. Coding
  3. JavaScript
  4. Solution for ClientProtocolException Caused by CircularRedirectException

Solution for ClientProtocolException Caused by CircularRedirectException

By 
Harsha Vardhan user avatar
Harsha Vardhan
·
Aug. 26, 14 · Code Snippet
Likes (2)
Comment
Save
Tweet
Share
44.7K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Object (computer science) Requests Snippet (programming) Execution (computing) Protocol (object-oriented programming) Signal Strings Clear (Unix)

Opinions expressed by DZone contributors are their own.

Related

  • Implementing WOPI Protocol For Office Integration
  • Writing DTOs With Java8, Lombok, and Java14+
  • Efficient String Formatting With Python f-Strings
  • Java String: A Complete Guide With Examples

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: