Anon Ymous
Anon Ymous

Reputation: 177

Get whole html code - Java?

I wanted to write a code that prints out the whole html code from a website, so I could get information about a certain player. My Problem now is:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


public class DownloadPage {

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


    URL url = new URL("http://apps.runescape.com/runemetrics/app/levels/player/Gragoyle");

    URLConnection con = url.openConnection();
    InputStream is =con.getInputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line = null;

    // read each line and write to System.out
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}
}

When i run this code it only prints the overview:

<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>

Id be very grateful if you could explain me how I can print the whole html code, and what I did wrong.

Upvotes: 1

Views: 554

Answers (1)

pr0gramista
pr0gramista

Reputation: 9008

Three problems:

  1. What you get from http://apps.runescape.com/runemetrics/app/levels/player/Gragoyle is a redirection to https://apps.runescape.com/runemetrics/app/levels/player/Gragoyle. This redirection is used to force users to connect by HTTPS.

  2. If you try to get data from https://apps.runescape.com/runemetrics/app/levels/player/Gragoyle you will get an SSL exeception. You can see more about it on: StackOverflow question. If you resolve this (fe. by accepting all certificates, not recommended in production) you will get HTML file, but it wouldn't be useful, because there is no player data on it.

  3. The data you actually want to get is retrieved by Javascript and AJAX calls. This is a great information for you, because you if you resolve problems with SSL you can get player data as JSON file, by calling fe.

https://apps.runescape.com/runemetrics/profile/profile?user=Gragoyle&activities=20

Then you can use any JSON parser fe. Gson to easily get values you want.

Note: To view JSON file in nice and readable form you can use this site or some plugin for your browser like JSONView for Chrome.

Upvotes: 1

Related Questions