Example usage for org.apache.commons.httpclient.util HttpURLConnection setInstanceFollowRedirects

List of usage examples for org.apache.commons.httpclient.util HttpURLConnection setInstanceFollowRedirects

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.util HttpURLConnection setInstanceFollowRedirects.

Prototype

public void setInstanceFollowRedirects(boolean paramBoolean) 

Source Link

Usage

From source file:org.openimaj.web.scraping.images.TmblrPhotoConsumer.java

/**
 * handles the variety of ways a tumblr addresses can be forwarded to
 * //from   w w  w  . jav  a2 s.c  o  m
 * @param url
 * @return
 * @throws IOException
 */
private String getPostID(URL url) throws IOException {
    final String host = url.getHost();
    URL loc = url;

    if (host.equals("tmblr.co") || host.equals("tumblr.com") || host.equals("www.tumblr.com")) {
        URL forwardURL = null;
        if (url.getHost().equals("tmblr.co")) {
            final String tumblrCode = url.getPath();
            forwardURL = new URL("http://www.tumblr.com" + tumblrCode);
        } else {
            forwardURL = url;
        }
        // now get the location header
        final HttpURLConnection con = (HttpURLConnection) forwardURL.openConnection();
        con.setInstanceFollowRedirects(false);
        final String locStr = con.getHeaderField("Location");
        loc = new URL(locStr);
        con.disconnect();
    }

    // Now extract the post ID from the actual tumblr address
    final String[] parts = loc.getPath().split("[/]");
    String postID = null;
    for (int i = 0; i < parts.length; i++) {
        if (parts[i].equals("post")) {
            postID = parts[i + 1];
            break;
        }
    }
    return postID;
}