从java中获取URL中的页面内容

时间:2015-03-03 05:02:55

标签: java url bufferedreader

无法访问此页面的内容" kissanime.com" (通过此代码从URL返回任何内容):

String a="http://kissanime.com";
    url = new URL(a);

    URLConnection conn = url.openConnection();

try ( // open the stream and put it into BufferedReader
        BufferedReader br = new BufferedReader(
        new InputStreamReader(conn.getInputStream()))) {
    String inputLine;
    while ((inputLine = br.readLine()) != null) {
        System.out.println(inputLine);
    }
}

1 个答案:

答案 0 :(得分:3)

如上所述,您需要通过 setRequestProperty 方法设置用户代理标头,如下所示。

    String a = "http://kissanime.com";
    URLConnection connection = new URL(a).openConnection();
    connection
            .setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
    connection.connect();

    BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream(),
            Charset.forName("UTF-8")));

    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = r.readLine()) != null) {
        sb.append(line);
    }
    System.out.println(sb.toString());

现在你会得到某事 !!