How can we load an HTML page using JAVA?

How can we load an HTML page using JAVA ??

+4
source share
3 answers

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 another in a while loop.

+10
source

If you have more requirements, such as authentication, you can use HttpClient

+2
source

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

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

All Articles