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?
Asked
Active
Viewed 166 times
1

user3049183
- 136
- 1
- 3
- 16
-
do you want to detect the url redirection after URLConnection con = url.openConnection(); – Yogesh Patil Jan 20 '16 at 06:50
-
3Possible duplicate of [Java - How to find the redirected url of a url?](http://stackoverflow.com/questions/2659000/java-how-to-find-the-redirected-url-of-a-url) – thegauravmahawar Jan 20 '16 at 06:53
2 Answers
2
Simply call getUrl()
on URLConnection
instance after calling getInputStream()

Mohammad Faisal
- 5,783
- 15
- 70
- 117

Yogesh Patil
- 908
- 4
- 14
1
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();

John
- 61
- 3
-
what is the point of `InputStream is = con.getInputStream();`? you even did not use it. and you dont have to. Just write `con.getInputStream();` – user3049183 Jan 20 '16 at 06:55
-
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. – John Jan 20 '16 at 06:56