Given a URL (String ref), I am attempting to retrieve the redirected URL as follows:
HttpURLConnection con = (HttpURLConnection)new URL(ref).openConnection();
con.setInstanceFollowRedirects(false);
con.setRequestProperty("User-Agent","");
int responseType = con.getResponseCode()/100;
while (responseType == 1)
{
Thread.sleep(10);
responseType = con.getResponseCode()/100;
}
if (responseType == 3)
return con.getHeaderField("Location");
return con.getURL().toString();
I am having several (conceptual and technical) problems with it:
Conceptual problem:
- It works in most cases, but I don't quite understand how.
- All methods of the 'con' instance are called AFTER the connection is opened (when 'con' is instanciated).
- So how do they affect the actual result?
- How come calling 'setInstanceFollowRedirects' affects the returned value of 'getHeaderField'?
- Is there any point calling 'getResponseCode' over and over until the returned value is not 1xx?
- Bottom line, my general question here: is there another request/response sent through the connection every time one of these methods is invoked?
Technical problem:
- Sometimes the response-code is 3xx, but 'getHeaderField' does not return the "final" URL.
- I tried calling my code with the returned value of 'getHeaderField' until the response-code was 2xx.
- But in most other cases where the response-code is 3xx, 'getHeaderField' DOES return the "final" URL, and if I call my code with this URL then I get an empty string.
Can you please advise how to approach the two problems above in order to have a "100% proof" code for retrieving the "final" URL?
Please ignore cases where the response-code is 4xx or 5xx (or anything else other than 1xx / 2xx / 3xx for that matter).
Thanks