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?
-
do you want to detect the url redirection after URLConnection con = url.openConnection();Yogesh Patil– Yogesh Patil2016-01-20 06:50:05 +00:00Commented Jan 20, 2016 at 6:50
-
3Possible duplicate of Java - How to find the redirected url of a url?thegauravmahawar– thegauravmahawar2016-01-20 06:53:45 +00:00Commented Jan 20, 2016 at 6:53
Add a comment
|
2 Answers
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();
2 Comments
user3049183
what is the point of
InputStream is = con.getInputStream();? you even did not use it. and you dont have to. Just write con.getInputStream();John
You are kind a interacting with the source ( this way you are knowing the precise end point ). So if it is redicted, con.getURL() will be different.