Retrieve the final location of a given URL in Java

Viewed 4493

I am trying to retrieve the final location of a given URL (String ref) as follows:

        HttpURLConnection con = (HttpURLConnection)new URL(ref).openConnection();
        con.setInstanceFollowRedirects(true);
        con.setRequestProperty("User-Agent","");
        int responseCode = con.getResponseCode();
        return con.getURL().toString();

It works in most cases, but rarely returns a URL which yet contains another redirection.

What am I doing wrong here?

Why do I get responseCode = 3xx, even after calling setInstanceFollowRedirects(true)?

UPDATE:

OK, responseCode can sometimes be 3xx.

If it happens, then I will return con.getHeaderField("Location") instead.

The code now is:

        HttpURLConnection con = (HttpURLConnection)new URL(ref).openConnection();
        con.setInstanceFollowRedirects(true);
        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();

Will appreciate comment should anyone see anything wrong with the code above.

UPDATE

  • Removed the handling of code 1xx, as according to most commenters it is not necessary.
  • Testing if the Location header exists before returning it, in order to handle code 304.

        HttpURLConnection con = (HttpURLConnection)new URL(ref).openConnection();
        con.setInstanceFollowRedirects(true);
        con.setRequestProperty("User-Agent","");
        if (con.getResponseCode()/100 == 3)
        {
            String target = con.getHeaderField("Location");
            if (target != null)
                return target;
        }
        return con.getURL().toString();
    
5 Answers

Sometime it is loading in the field of requestURI. Use like this code:

val declaredField = con.javaClass.getDeclaredField("requestURI")
declaredField.isAccessible=true
val loc = declaredField.get(con).toString()
Related