Amandeep Gupta
Amandeep Gupta

Reputation: 161

Get page content from URL in java

Not able to access content of this page "kissanime.com" (it's not returning anything) from URL by this code :

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

Upvotes: 0

Views: 8904

Answers (1)

Cataclysm
Cataclysm

Reputation: 8558

As my above comment , you need to set user agent header by setRequestProperty method as below.

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

Now you will get somethings !!

Upvotes: 5

Related Questions