I'm using HttpURLConnection
to retrieve an URL just like that:
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(true);
// ...
I now want to find out if there was a redirect and if it was a permanent (301) or temporary (302) one in order to update the URL in the database in the first case but not in the second one.
Is this possible while still using the redirect handling of HttpURLConnection
and if, how?
Simply call
getUrl()
onURLConnection
instance after callinggetInputStream()
:If you need to know whether the redirection happened before actually getting it's contents, here is the sample code: