Skip to content Skip to sidebar Skip to footer

How We Can Download A Html Page Using Java?

How we can Download a HTML Page using JAVA??

Solution 1:

Here is the code:

publicstatic String savePage(final String URL)throws IOException {
    Stringline="", all = "";
    URLmyUrl=null;
    BufferedReaderin=null;
    try {
        myUrl = newURL(URL);
        in = newBufferedReader(newInputStreamReader(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.

Solution 2:

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

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

Solution 3:

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

Post a Comment for "How We Can Download A Html Page Using Java?"