Search code examples
javaurlhttpurlconnectionhttpconnection

How to get new URL when the input URL changes?


Consider this URL http://dx.doi.org/10.1006/jpdc.1997.1383. When I put it in the browser address bar and press enter, the URL will change into http://www.sciencedirect.com/science/article/pii/S0743731597913836. Using Java, how can I get the second URL address?


Solution

  • URLConnection con = new URL( url ).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();