2013-02-19 8 views
7

Ich habe eine URL, die auf eine andere url.I umleitet wollen die endgültige umgeleitet URL.My Code erhalten können:java URLConnection erhalten die endgültige URL umgeleitet

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(); 

} }

Es gibt immer original url, während die redirectURL ist: 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.

Wie kann ich diese endgültige umgeleitete URL erhalten.

Hier ist, was ich mit Looping versucht, bis wir redirects.Still bekommen doesent die gewünschte URL holen:

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 Suprise Surprise - Ich bin das gleiche Verhalten auf meinem Rechner nicht immer - MACOSX .. Ich erhalte den umgelenkten Wert ....... .......... orignal url: http://tinyurl.com/KindleWireless verbundene URL: http://tinyurl.com/KindleWireless umgeleitete URL: http://www.amazon.com/Kindle -Keyboard-Free-Wi-Fi-Anzeige/dp/B004HZYA6E – user1428716

+0

aber die Weiterleitung URL, die wir bekommen, ist nicht die endgültige URL.Finale URL ist was ich eingefügt.Wenn Sie die tinyUrl in Browser Sie dann endgültige URL eingeben Sie ist: http : //www.ama zon.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 – Jeets

+0

@Jeets haben u Antwort auf Ihre question.because erhalten i .. – dipali

Antwort

2

Meine erste Idee wäre instanceFollowRedirects auf false gesetzt oder mit URLConnection statt.

In beiden Fällen wird die Weiterleitung nicht ausgeführt, daher erhalten Sie eine Antwort auf Ihre ursprüngliche Anfrage. Rufen Sie den HTTP-Statuswert ab, und wenn es 3xx ist, erhalten Sie den neuen Umleitungswert.

Natürlich kann es eine Kette von Weiterleitungen geben, also werden Sie wahrscheinlich iterieren wollen, bis Sie die echte (Status 2xx) Seite erreichen.

+0

gleiche Problem konfrontiert bin ich schon versucht, diese aber immer noch die richtigen url.I bearbeitet und hinzugefügt geben dosent was ich nach Ihrem Vorschlag in der ursprünglichen Post versucht – Jeets

1

@ user719950 auf meinem Mac-OSX - dies das Problem der verkürzten HTTP-URL löst:

um Ihre ursprünglichen Code, fügen Sie einfach diese unterhalb der Linie: // Sie haben über Ihren Browser zu finden, was ist der Request-Header IE/Chrome sendet. Ich habe nicht noch die Erklärung haben, wie, warum diese einfache Einstellung korrekte URL verursacht :)

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

Diese

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

} 
helfen könnten
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

Der Code, den ich oben geschrieben habe, wird alle umgeleiteten URLs drucken. – aasha

0

@JEETS Ihre fetchRedirectURL Funktion kann nicht funktionieren, da es einen sind Vielzahl von HTTP-Codes für Weiterleitungen. Ändern Sie es in eine Bereichsüberprüfung und es wird funktionieren.

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

Dieses geht rekursiv, falls es mehrere Umleitungen:

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

die Sie interessieren, ich mit rekursiv für viele Umleitungs-URL zu verwenden.

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

und mit:

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

} 
Verwandte Themen