Java nhận url chuyển hướng cuối cùng

@Test public void testRedirect[] ném Ngoại lệ { // Chỉ vô hiệu hóa các chuyển hướng sau cho thử nghiệm này boolean originalValue = HttpURLConnection. getFollowRedirects[]; . setFollowRedirects[false]; . 0"]; tổ chức. apache. xúc xích. Gốc ngữ cảnh = tomcat. addWebapp[null, "", rootDir. getAbsolutePath[]]; . addPattern["/welcome-files/*"]; . addPattern["/tệp chào mừng"]; . addCollection[bộ sưu tập]; . addAuthRole["foo"]; . addConstraint[ràng buộc]; . apache. xúc xích. Ví dụ ngữ cảnh = tomcat. addWebapp [ null, "/ ví dụ", ví dụDir. getAbsolutePath[]]; . setMapperContextRootRedirectEnabled[false]; . setDeny[". *"]; rav. setDenyStatus[404]; . getPipeline[]. addValve[rav]; . bắt đầu[]; . setFollowRedirects[ originalValue];

Chính sách chuyển hướng tự động được kiểm tra bất cứ khi nào nhận được mã phản hồi

    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="//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. Nếu chuyển hướng không tự động xảy ra, thì phản hồi, chứa mã phản hồi
    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="//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, được trả về, nơi nó có thể được xử lý thủ công

Chính sách

    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="//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];

    }
2 được thiết lập thông qua Builder. phương thức followRedirects

Tôi có một url chuyển hướng đến một url khác. Tôi muốn có thể nhận được URL được chuyển hướng cuối cùng. Mã của tôi

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

    HttpURLConnection con =[HttpURLConnection] new URL[ "//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[];

}
}

Nó luôn cung cấp url gốc trong khi redirectURL là. http. //www. amazon. com/kindle-wiress-reading-display-globally/dp/b003fsudm4/ref = amb_link_353259562_2?

Làm cách nào tôi có thể nhận được URL được chuyển hướng cuối cùng này

Đây là những gì tôi đã thử với vòng lặp cho đến khi chúng tôi nhận được chuyển hướng. Vẫn không tìm nạp url mong muốn

    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="//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];

    }

Giải pháp tốt nhất

Hãy thử điều này, tôi sử dụng đệ quy để sử dụng cho nhiều URL chuyển hướng

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

và sử dụng

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

}

Giải pháp liên quan

Java – Sự khác biệt giữa HashMap và Hashtable trong Java là gì

Có một số khác biệt giữa

    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="//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];

    }
3 và
    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="//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 trong Java

  1.     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="//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 được đồng bộ hóa, trong khi
        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="//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];
    
        }
    
    3 thì không. Điều này làm cho
        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="//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];
    
        }
    
    3 tốt hơn cho các ứng dụng không theo luồng, vì các Đối tượng không được đồng bộ hóa thường hoạt động tốt hơn các đối tượng được đồng bộ hóa

  2.     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="//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 không cho phép khóa hoặc giá trị của
        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="//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.
        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="//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];
    
        }
    
    3 cho phép một khóa
        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="//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 và bất kỳ số lượng giá trị
        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="//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 nào

  3. Một trong các lớp con của HashMap là

        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="//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, vì vậy trong trường hợp bạn muốn thứ tự lặp lại có thể dự đoán được [thứ tự chèn theo mặc định], bạn có thể dễ dàng hoán đổi
        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="//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];
    
        }
    
    3 lấy
        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="//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. Điều này sẽ không dễ dàng nếu bạn đang sử dụng
        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="//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

Vì đồng bộ hóa không phải là vấn đề đối với bạn, tôi khuyên bạn nên dùng

    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="//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];

    }
3. Nếu đồng bộ hóa trở thành vấn đề, bạn cũng có thể xem
    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="//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];

    }
9

Chủ Đề