Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HttpURLConnection: Handle HTTP redirects

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?

like image 349
Julian Avatar asked Apr 02 '13 01:04

Julian


People also ask

How to handle redirect response in java?

The sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file. It accepts relative as well as absolute URL. It works at client side because it uses the url bar of the browser to make another request.

What is HTTP URL connection in Android?

HttpsURLConnection. HttpsURLConnection. HttpsURLConnection extends HttpURLConnection with support for https-specific features. The abstract class URLConnection is the superclass of all classes that represent a communications link between the application and a URL.

What is setInstanceFollowRedirects?

setInstanceFollowRedirects(boolean followRedirects) Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this HttpURLConnection instance. void. setRequestMethod(String method)

How to fetch redirect URL in java?

How do you find the real URL behind the redirect? You can do this in Java using the HttpURLConnection class. Like this: String url = "http://bit.ly/23414"; HttpURLConnection con = (HttpURLConnection) new URL(url).


2 Answers

Simply call getUrl() on URLConnection instance after calling getInputStream():

URLConnection con = new URL(url).openConnection();
System.out.println("Orignal URL: " + con.getURL());
con.connect();
System.out.println("Connected URL: " + con.getURL());
InputStream is = con.getInputStream();
System.out.println("Redirected URL: " + con.getURL());
is.close();

If you need to know whether the redirection happened before actually getting it's contents, here is the sample code:

HttpURLConnection con = (HttpURLConnection) (new URL(url).openConnection());
con.setInstanceFollowRedirects(false);
con.connect();
int responseCode = con.getResponseCode();
System.out.println(responseCode);
String location = con.getHeaderField("Location");
System.out.println(location);
like image 61
syb0rg Avatar answered Oct 05 '22 10:10

syb0rg


private HttpURLConnection openConnection(String url) throws IOException {
    HttpURLConnection connection;
    boolean redirected;
    do {
        connection = (HttpURLConnection) new URL(url).openConnection();
        int code = connection.getResponseCode();
        redirected = code == HTTP_MOVED_PERM || code == HTTP_MOVED_TEMP || code == HTTP_SEE_OTHER;
        if (redirected) {
            url = connection.getHeaderField("Location");
            connection.disconnect();
        }
    } while (redirected);
    return connection;
}
like image 21
danik Avatar answered Oct 05 '22 10:10

danik