0

The follwing url gives response code 301 when I run it through Java.The url is: http://www.amazon.com/Kindle-Wireless-Reading-Display-Globally/dp/B003FSUDM4/ref=amb_link_353259562_2?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-10&pf_rd_r=11EYKTN682A79T370AM3&pf_rd_t=201&pf_rd_p=1270985982&pf_rd_i=B002Y27P3M and the redirect url it gives as per java code is:

http://www.amazon.com/Kindle-Keyboard-Free-Wi-Fi-Display/dp/B004HZYA6E

Code is :

public class newTestURLConnection {
    public static void main(String[] args) throws IOException {
        String url="http://www.amazon.com/Kindle-Wireless-Reading-Display-Globally/dp/B003FSUDM4/ref=amb_link_353259562_2?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-10&pf_rd_r=11EYKTN682A79T370AM3&pf_rd_t=201&pf_rd_p=1270985982&pf_rd_i=B002Y27P3M";
        HttpURLConnection con =(HttpURLConnection) new URL( url ).openConnection();
        con.setInstanceFollowRedirects(false);

        con.connect();

        InputStream is = con.getInputStream();
        System.out.println(con.getHeaderField("Location"));
        System.out.println( "Response code: " + con.getResponseCode() );
    }
}

But when I open url in browser it does not redirect to:http://www.amazon.com/Kindle-Keyboard-Free-Wi-Fi-Display/dp/B004HZYA6E. Could anyone suggest why this difference?

koopajah
  • 23,792
  • 9
  • 78
  • 104
Jeets
  • 3,189
  • 8
  • 34
  • 50

2 Answers2

1
instanceFollowRedirects

protected boolean instanceFollowRedirects

    If true, the protocol will automatically follow redirects. If false, the protocol will not automatically follow redirects.

    This field is set by the setInstanceFollowRedirects method. Its value is returned by the getInstanceFollowRedirects method.

    Its default value is based on the value of the static followRedirects at HttpURLConnection construction time. 

This is from Class HttpURLConnection Docs

SSC
  • 2,956
  • 3
  • 27
  • 43
0

Could anyone suggest why this difference?

It could be that the Amazon is responding differently based on differences in the request headers; e.g. what is being sent in the User-Agent header.

Perhaps this is about showing different results to real customers and to web crawlers / content scrapers.

Whatever the reason, the difference between the two pages is very subtle ... at least to the human eye.


If this is not just a once-off thing (e.g. testing some code), I should also ask whether you have read the Amazon "Conditions of Use"; e.g. the bit where it says:

"No Amazon Service, nor any part of any Amazon Service, may be reproduced, duplicated, copied, sold, resold, visited, or otherwise exploited for any commercial purpose without express written consent of Amazon."

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216