views:

99

answers:

3

How we can Download a HTML Page using JAVA??

+1  A: 

Here is the code:

URL myUrl = new URL("http://www.somepage.com/");
    BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                        myUrl.openStream()));

    String line;

    while ((line = in.readLine()) != null)
        System.out.println(line);

    in.close();

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

Klark
@SiNTER :can i add this stuff into my MySQL? how can i do that?
Of course you can. You can build some sting buffer from each line and put it in the database.
Klark
@SiNter : Thks...
A: 

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

rlovtang
@rlovtang : No i just need to fetch the page and download it to my database, as for web index
Ok, then HttpClient is probably overkill
rlovtang
@rlovtang : what did that mean?
I mean that you will probably do fine with SINTER's solution. You can of course use HttpClient if you like.
rlovtang
+1  A: 

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
rlovtang