Java URL Redirect getRedirectUrl(final URL url)

Here you can find the source of getRedirectUrl(final URL url)

Description

Sends a request to the given URL and checks, if it contains a redirect.

License

Open Source License

Parameter

Parameter Description
url a parameter

Return

- The redirect URL.

Declaration

public static URL getRedirectUrl(final URL url) 

Method Source Code

//package com.java2s;
/**//from  w  ww.  j  a v a  2  s .co  m
 *
 *  BibSonomy-Web-Common - A blue social bookmark and publication sharing system.
 *
 *  Copyright (C) 2006 - 2011 Knowledge & Data Engineering Group,
 *                            University of Kassel, Germany
 *                            http://www.kde.cs.uni-kassel.de/
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.io.IOException;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**
     * maximal number of redirects to follow in {@link #getRedirectUrl(URL)}
     */
    private static final int MAX_REDIRECT_COUNT = 10;
    /**
     * The user agent used for all requests with {@link HttpURLConnection}.
     */
    private static final String USER_AGENT_PROPERTY_VALUE = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)";
    private static final String LOCATION = "Location";
    private static final String USER_AGENT_HEADER_NAME = "User-Agent";

    /**
     * Sends a request to the given URL and checks, if it contains a redirect.
     * If it does, returns the redirect URL. Otherwise, returns null.
     * This is done up to {@value #MAX_REDIRECT_COUNT}-times until the final page is reached. 
     *  
     * 
     * 
     * @param url
     * @return - The redirect URL.
     */
    public static URL getRedirectUrl(final URL url) {
        try {
            URL internalUrl = url;
            URL previousUrl = null;
            int redirectCtr = 0;
            while (internalUrl != null && redirectCtr < MAX_REDIRECT_COUNT) {
                redirectCtr++;

                final HttpURLConnection urlConn = (HttpURLConnection) internalUrl.openConnection();

                urlConn.setAllowUserInteraction(false);
                urlConn.setDoInput(true);
                urlConn.setDoOutput(false);
                urlConn.setUseCaches(false);
                urlConn.setInstanceFollowRedirects(false);

                /*
                 * set user agent (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) since some 
                 * pages require it to download content.
                 */
                urlConn.setRequestProperty(USER_AGENT_HEADER_NAME, USER_AGENT_PROPERTY_VALUE);

                urlConn.connect();

                // get URL to redirected resource
                previousUrl = internalUrl;
                try {
                    internalUrl = new URL(urlConn.getHeaderField(LOCATION));
                } catch (final MalformedURLException e) {
                    internalUrl = null;
                }

                urlConn.disconnect();

            }

            return previousUrl;
        } catch (final IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. getRedirectedUrl(String url)