2013-02-19 41 views
7

我有一個重定向到另一個url.I一個網址希望能夠獲得最後的重定向URL.My代碼:的java的URLConnection得到最終的重定向的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(); 

}}

它總是給出原始的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=11EYKTN682A79T370AM3&pf_rd_t=201&pf_rd_p=1270985982&pf_rd_i=B002Y27P3M

我怎樣才能得到這個最終重定向的URL。

這裏是我試圖與循環,直到我們得到redirects.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); 

    } 
+0

@ SJuan76驚喜驚喜 - 我沒有得到我的機器上相同的行爲 - MACOSX ..我正在重新定向值....... .......... orignal url:http://tinyurl.com/KindleWireless 連接網址:http://tinyurl.com/KindleWireless 重定向網址:http://www.amazon.com/Kindle -Keyboard-Free-Wi-Fi-Display/dp/B004HZYA6E – user1428716 2013-02-19 07:23:49

+0

但我們得到的重定向url不是最終的url。最終的url是我粘貼的。如果你將tinyUrl粘貼到瀏覽器中,那麼你得到的最終url是:http ://www.ama zon.com/Kindle-Wireless-Reading-Display-Globally/dp/B003FSUDM4/ref=amb_link_353259562_2?pf_rd_m = ATVPDKIKX0DER&pf_rd_s =中心10 pf_rd_r = 11EYKTN682A79T370AM3&pf_rd_t = 201&pf_rd_p = 1270985982&pf_rd_i = B002Y27P3M – Jeets 2013-02-19 07:54:48

+0

@Jeets具有u讓你的question.because的答案我面臨同樣的問題.. – dipali 2015-10-28 07:15:42

回答

2

我的第一個想法是設置instanceFollowRedirects爲false,或使用URLConnection代替。

在這兩種情況下,重定向都不會被執行,因此您將收到原始請求的回覆。獲取HTTP狀態值,如果它是3xx,則獲取新的重定向值。

當然,可能會有一連串的重定向,所以可能你會想迭代,直到你到達真實(狀態2xx)頁面。

+0

我試過,但仍然dosent給編輯和添加正確的url.I我在原帖 – Jeets 2013-02-19 08:59:30

1

@ user719950在我的MAC-OSX - 這解決了截斷HTTP URL的問題:

你原來的代碼,只需添加此線以下://你必須通過瀏覽器來查找是請求頭IE/Chrome正在發送。我仍然不具有解釋爲什麼這個簡單的設置導致正確的網址:)

HttpURLConnection con =(HttpURLConnection) new URL 
("http://tinyurl.com/KindleWireless").openConnection(); 
con.setInstanceFollowRedirects(true); 
con.setDoOutput(true); 
    System.out.println("orignal url: " + con.getURL());  
     **con.setRequestProperty("User-Agent", 
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) 
    AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 
    Safari/536.26.17");**     

      con.connect(); 
    System.out.println("connected url: " + con.getURL()); 
    Thread.currentThread().sleep(2000l); 
    InputStream is = con.getInputStream(); 
    System.out.println("redirected url: " + con.getURL()); 

    is.close(); 
1

這可能有助於

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

HttpURLConnection con = (HttpURLConnection) new URL(
     "http://tinyurl.com/KindleWireless").openConnection(proxy); 
    System.out.println("orignal url: " + con.getURL()); 
    con.connect(); 
    con.setInstanceFollowRedirects(false); 
    int responseCode = con.getResponseCode(); 
    if ((responseCode/100) == 3) { 
     String newLocationHeader = con.getHeaderField("Location"); 
     responseCode = con.getResponseCode(); 
     System.out.println("Redirected Location " + newLocationHeader); 
     System.out.println(responseCode); 
    } 

} 
3
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; 
} 
+0

中按照您的建議嘗試了我在上面發佈的代碼將打印所有重定向的網址。 – aasha 2013-09-16 05:45:05

0

@JEETS 你fetchRedirectURL功能可能無法正常工作,因爲有各種HTTP代碼重定向。將其更改爲範圍檢查,它將起作用。

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()>=300 && con.getResponseCode() <400) 
    return con.getHeaderField("Location"); 
else return null; 
    } 
0

這人去遞歸的情況下有多個重定向:

protected String getDirectUrl(String link) { 
    String resultUrl = link; 
    HttpURLConnection connection = null; 
    try { 
     connection = (HttpURLConnection) new URL(link).openConnection(); 
     connection.setInstanceFollowRedirects(false); 
     connection.connect(); 
     int responseCode = connection.getResponseCode(); 
     if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { 
      String locationUrl = connection.getHeaderField("Location"); 

      if (locationUrl != null && locationUrl.trim().length() > 0) { 
       IOUtils.close(connection); 
       resultUrl = getDirectUrl(locationUrl); 
      } 
     } 
    } catch (Exception e) { 
     log("error getDirectUrl", e); 
    } finally { 
     IOUtils.close(connection); 
    } 
    return resultUrl; 
} 
12

試試這個,我用遞歸地使用了許多重定向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; 
} 

和使用:

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

} 
相關問題