Finding the Redirected URL

M

Muggle

Hello everyone,

I have a url http://www.xyz.com and in the browser it gets redirected
to http://www.xyz.com/page_id=123.

Using java, how can I start from www.xyz.com and find out the
redirected url ? (www.xyz.com/page_id=123 in this case) ?

I started in the following way, but could not get there. The response
code even for a redirecting URL is 200. Why is that?
.....
String urlString = "http://www.ft.com";
URL url = new URL(urlString);
HttpURLConnection httpURLConnection = (HttpURLConnection)
url.openConnection();
httpURLConnection.setInstanceFollowRedirects(true);
System.out.println("Response Code " +
httpURLConnection.getResponseCode());

Thank you
Muggle
 
S

Stefan Ram

Muggle said:
Using java, how can I start from www.xyz.com and find out the
redirected url ? (www.xyz.com/page_id=123 in this case) ?

public class Main
{ public static void main( final java.lang.String[] args )
throws java.lang.Throwable
{ final java.lang.CharSequence uri = "http://www.purl.org/stefan_ram/";
final java.net.URL url = new java.net.URL( uri.toString() );
final java.net.HttpURLConnection httpURLConnection =
( java.net.HttpURLConnection )url.openConnection();
httpURLConnection.setInstanceFollowRedirects( false );
httpURLConnection.connect();
final int responseCode = httpURLConnection.getResponseCode();
java.lang.System.out.println( responseCode );
final java.lang.String header =
httpURLConnection.getHeaderField( "Location" );
java.lang.System.out.println( header ); }}

/* prints:
302
http://userpage.fu-berlin.de/~ram/pub/pub_jf47ht81Ht/
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top