Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Unexpected redirection after GET request  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My english is poor, but I tried to describe the problem as much clear as possible.

I try to make GET request using HttpClient from Apache Http Client.
Every time I try to do it using Apache Http Client, remote server redirects me with "301 Moved Permanently".
Redirection doesn't occur when I use URLConnection or curl (through command line or php).
I guess that there are some predefined settings in Apache Http Client that cause remote server to redirect me.

I get this error:


Code that doesn't work:


Debug info:


None of the solutions from the internet works.

But when I use plain URLConnection then it works and no redirection occurs.
Code that works:


What is wrong with Apache Http Client?

By the way, using curl in php it also works fine without any redirection:
 
Saloon Keeper
Posts: 5496
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you omit setting a redirect strategy?
 
David Belcher
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:What happens if you omit setting a redirect strategy?


If I omit, it still does not work (It redirects to the same address):


It is very incomprehensible to me that even Jsoup works fine:
 
Bartender
Posts: 20766
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A HTTP 301 - Moved Permanently response is very common and while it can be used manually to indicate an actual re-located web page, it seems to be a common routing mechanism in normal configurations as well.

Some clients will recognize the Location given in the 301 response and re-issue the request, making it look like the request went straight through. Other clients, however, will dead-end.

The curl utility can be told to do either of the above, given the right command-line options.

Incidentally, while I'm not 100% sure, I think that "301" is used in JavaServer Faces to enforce security. Since container-managed security is based on the incoming HTTP URL and normal JSF operation uses the URL as a handle, and not as an absolute page locator, you have to use the JSF REDIRECT option to re-route to the protected URL and thereby ensure that the underlying resource has the proper access rules applied. And JSF REDIRECT is - I'm reasonably certain - done by returning an HTTP 301. It's extra overhead, but beats having your payroll data end up for sale on the Internet.
 
Saloon Keeper
Posts: 2590
327
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Belcher wrote:Every time I try to do it using Apache Http Client, remote server redirects me with "301 Moved Permanently"


I noticed that if you try to establish an http connection, rather than an https, that it will behave as you describe - trying to upgrade the security of the connection.  Is it possible that something in your connection path might be rewriting the URL to use http?

When I run your code as-is (with httpclient version 4.5.6), it works as expected, and I see the following output:
Here is the logging:
These are the properties I used to enable the logging:
 
Ron McLeod
Saloon Keeper
Posts: 2590
327
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Belcher wrote:But when I use plain URLConnection then it works and no redirection occurs.


Sorry - I didn't notice the comments about no being an issue with HttpURLConnection.

Which version of HttpClient are you using?
 
Tim Holloway
Bartender
Posts: 20766
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point.

I have my servers set up so that if you request an http URL, they will re-direct to https. And they're probably using a 301 response to do it.
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!