简体   繁体   中英

How can I read an html code from Amazon by using URL or any other method ? 503 Code Appears

When I try to compile this code

 URL url = new URL("https://www.amazon.com");
  BufferedReader bufr = new BufferedReader(new InputStreamReader(url.openStream()));
  String data;
  while ((data=bufr.readLine())!=null)
  System.out.println(data);

It says: java.io.IOException: Server returned HTTP response code: 503 for URL: https://www.amazon.com
How can I search for a word in amazon url?

I read couple of links got to know that User-Agent value needs to be added to fix 503 error. Below is the sample code.

        URL url = new URL("https://www.amazon.com");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

        BufferedReader bufr = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String data;
        while ((data = bufr.readLine()) != null)
            System.out.println(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM