301 Moved Permanently, URLConnection and HTTPURLConnection

If a website has applied a redirection from "BaseUrl" to "BaseUrl/" then what you see the response by hitting "" in your browser "301 Moved Permanently" and redirected to "/"

How to deal this situation when creating a URLConnection and know to what URL you have been redirected to?

If you specify
URLConnection con = new URL(uri).openConnection();


It would create a connection. Later if you try to get the URL from the "con" object.
con.getURL().toString();


This would give you the "BaseUrl" and you would not get the redirected URL.

But if you create a HttpURLConnection instead of URLConnection.
HttpURLConnection httpURLCon = (HttpURLConnection) new URL(uri).openConnection();
then if you try to get the URL it would give the correct redirected URL "BaseUrl/"

If anyone does get contradictory results, please do let me know.

Comments

Popular posts from this blog

Hibernate: a different object with the same identifier value was already associated with the session

BeanDefinitionStoreException: Failed to parse configuration class: Could not find class [javax.jms.ConnectionFactory]