9

別の URL にリダイレクトする URL があります。最終的にリダイレクトされた 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 は次のとおりです。 1270985982&pf_rd_i=B002Y27P3M .

この最終的なリダイレクト URL を取得するにはどうすればよいですか。

これは、リダイレクトを取得するまでループして試したものです。それでも、目的の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);

    }
4

7 に答える 7

20

これを試してみてください。私は再帰的に使用して、多くのリダイレクト 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);

}
于 2014-09-25T03:47:24.790 に答える
8
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;
}
于 2013-09-16T05:43:44.280 に答える
2

私の最初のアイデアはinstanceFollowRedirects、false に設定するか、URLConnection代わりに使用することです。

どちらの場合も、リダイレクトは実行されないため、元の要求に対する応答が返されます。HTTP ステータス値を取得し、3xx の場合は新しいリダイレクト値を取得します。

もちろん、一連のリダイレクトが発生する可能性があるため、実際の (ステータス 2xx) ページに到達するまで反復する必要があります。

于 2013-02-19T07:15:34.027 に答える
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);
    }

}
于 2013-02-19T09:54:42.980 に答える
1

@ user719950 私の MAC-OSX では、これにより切り捨てられた HTTP URL の問題が解決されます。

元のコードに、次の行を追加するだけです。この単純な設定が正しい URL を引き起こしている理由については、まだ説明がありません :)

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();
于 2013-02-19T09:12:03.387 に答える