Java URL Read getUrlFollowingRedirects(String possibleRedirectionUrl)

Here you can find the source of getUrlFollowingRedirects(String possibleRedirectionUrl)

Description

get Url Following Redirects

License

Open Source License

Declaration

private static URL getUrlFollowingRedirects(String possibleRedirectionUrl) throws IOException 

Method Source Code

//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;
    }
}

Related

  1. getURL(String url)
  2. getURL(URL url, String params)
  3. getUrlContent(String url)
  4. getURLContent_old(final String uri, final StringBuffer content)
  5. getUrlContentWithRetries(String url, long timeoutMs, long retryDelayMs)
  6. getUrlInfos(String urlAsString, int timeout)
  7. getUrlSource(String url)
  8. getUrlStatus(String url)
  9. getUrlTxt(String url)