读取URL不适用于每个URL

时间:2016-05-20 03:50:13

标签: inputstream bufferedreader urlconnection inputstreamreader

我使用以下方法来读取URL。它适用于大多数URL。但对于某些网址,它会出错。

作为http://www.brainyquote.com/quotes/keywords/father.html网址的示例,它给出了错误,

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)

我的代码是,

    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();
        }
    }

使用上述代码阅读每个网址的解决方案是什么?

0 个答案:

没有答案