Binu Mathew
Binu Mathew

Reputation:

How we can Download a HTML Page using JAVA?

How we can Download a HTML Page using JAVA??

Upvotes: 4

Views: 18169

Answers (3)

rlovtang
rlovtang

Reputation: 5060

If you have more requirements, like authentication, you can use HttpClient

Upvotes: 2

Klark
Klark

Reputation: 8280

Here is the code:

public static String savePage(final String URL) throws IOException {
    String line = "", all = "";
    URL myUrl = null;
    BufferedReader in = null;
    try {
        myUrl = new URL(URL);
        in = new BufferedReader(new InputStreamReader(myUrl.openStream()));

        while ((line = in.readLine()) != null) {
            all += line;
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }

    return all;
}

Now you can process one line after one other in the while loop.

Upvotes: 10

rlovtang
rlovtang

Reputation: 5060

If you can use Groovy, which compiles to java bytecode, you can fetch a page like this:

String text = new URL("http://google.com").text

Upvotes: 2

Related Questions