java urlconnection获取最终重定向的URL

Jee*_*ets 8 java

我有一个重定向到另一个url的网址.我希望能够获得最终重定向的网址.我的代码:

    public class testURLConnection
    {
    public static void main(String[] args) throws MalformedURLException, IOException {

    HttpURLConnection con =(HttpURLConnection) new URL( "http://tinyurl.com/KindleWireless" ).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();
Run Code Online (Sandbox Code Playgroud)

}}

它始终提供原始URL,而redirectURL是:http://www.amazon.com/Kindle-Wireless-Reading-Display-Globally/dp/B003FSUDM4/ref=amb_link_353259562_2?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-10&pf_rd_r=11EYKT662A79T370AM3&pf_rd_t=201&pf_rd_p = 1270985982&pf_rd_i = B002Y27P3M.

如何获得此最终重定向的URL.

这是我尝试循环直到我们得到重定向.Still doesent获取所需的URL:

    public static String fetchRedirectURL(String url) throws IOException
    {
HttpURLConnection con =(HttpURLConnection) new URL( url ).openConnection();
//System.out.println( "orignal url: " + con.getURL() );
con.setInstanceFollowRedirects(false);
con.connect();


InputStream is = con.getInputStream();
if(con.getResponseCode()==301)
    return con.getHeaderField("Location");
else return null;
    }
    public static void main(String[] args) throws MalformedURLException, IOException {
String url="http://tinyurl.com/KindleWireless";
String fetchedUrl=fetchRedirectURL(url);
System.out.println("FetchedURL is:"+fetchedUrl);
while(fetchedUrl!=null)
{   url=fetchedUrl;
System.out.println("The url is:"+url);
    fetchedUrl=fetchRedirectURL(url);


}
System.out.println(url);

    }
Run Code Online (Sandbox Code Playgroud)

fre*_*wed 16

试试这个,我递归地使用许多重定向URL.

public static String getFinalURL(String url) throws IOException {
    HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
    con.setInstanceFollowRedirects(false);
    con.connect();
    con.getInputStream();

    if (con.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
        String redirectUrl = con.getHeaderField("Location");
        return getFinalURL(redirectUrl);
    }
    return url;
}
Run Code Online (Sandbox Code Playgroud)

和使用:

public static void main(String[] args) throws MalformedURLException, IOException {
    String fetchedUrl = getFinalURL("<your_url_here>");
    System.out.println("FetchedURL is:" + fetchedUrl);

}
Run Code Online (Sandbox Code Playgroud)


aas*_*sha 6

public static String getFinalRedirectedUrl(String url) {

    HttpURLConnection connection;
    String finalUrl = url;
    try {
        do {
            connection = (HttpURLConnection) new URL(finalUrl)
                    .openConnection();
            connection.setInstanceFollowRedirects(false);
            connection.setUseCaches(false);
            connection.setRequestMethod("GET");
            connection.connect();
            int responseCode = connection.getResponseCode();
            if (responseCode >= 300 && responseCode < 400) {
                String redirectedUrl = connection.getHeaderField("Location");
                if (null == redirectedUrl)
                    break;
                finalUrl = redirectedUrl;
                System.out.println("redirected url: " + finalUrl);
            } else
                break;
        } while (connection.getResponseCode() != HttpURLConnection.HTTP_OK);
        connection.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return finalUrl;
}
Run Code Online (Sandbox Code Playgroud)


SJu*_*n76 2

我的第一个想法是设置instanceFollowRedirects为 false,或者使用URLConnection它。

在这两种情况下,都不会执行重定向,因此您将收到对原始请求的回复。获取 HTTP 状态值,如果是 3xx,则获取新的重定向值。

当然,可能存在一系列重定向,因此您可能需要迭代,直到到达真正的(状态 2xx)页面。