Here you can find the source of getUrlFollowingRedirects(String possibleRedirectionUrl)
private static URL getUrlFollowingRedirects(String possibleRedirectionUrl) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; public class Main { private static URL getUrlFollowingRedirects(String possibleRedirectionUrl) throws IOException { URL url = new URL(possibleRedirectionUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int status = conn.getResponseCode(); if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) { // update the URL if we were redirected url = new URL(conn.getHeaderField("Location")); }//from w ww . j a v a2s .co m return url; } private static URL getUrlFollowingRedirects(URL url) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int status = conn.getResponseCode(); if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) { // update the URL if we were redirected url = new URL(conn.getHeaderField("Location")); } return url; } }