0

I used following method to read a URL. It works fine for most of the URLs. But for some URL it gave an error.

As a example for http://www.brainyquote.com/quotes/keywords/father.html URL it gave an error as,

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.brainyquote.com/quotes/keywords/father.html
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at test.TestURL.main(TestURL.java:22)

My code is,

    import java.net.*;
    import java.util.Scanner;
    import java.io.*;
    import javax.swing.text.Document;
    import org.jsoup.Jsoup;

    public class TestURL {

        public static void main(String[] args) throws Exception {

            URL x = new URL("http://www.brainyquote.com/quotes/keywords/father.html");

            URLConnection yc = x.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
            String inputLine;

            while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);

            in.close();
        }
    }

What is a solution for read every URL using above code?

Emalka
  • 381
  • 2
  • 4
  • 16

0 Answers0