java urlconnection get the final redirected URL

后端 未结 7 1808
死守一世寂寞
死守一世寂寞 2020-11-30 13:07

I have a url which redirects to another url.I want to be able to get the final redirected URL.My code:

    public class testURLConnection
    {
    public st         


        
7条回答
  •  情深已故
    2020-11-30 13:19

    This might help

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

提交回复
热议问题