Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something similar to WebClient.DownloadString in Java?

I want to download the html source code of a site to parse some info. How do I accomplish this in Java?

like image 233
Sergio Tapia Avatar asked Jan 20 '10 02:01

Sergio Tapia


1 Answers

Just attach a BufferedReader (or anything that reads strings) from a URL's InputStream returned from openStream().

public static void main(String[] args)
        throws IOException
{
    URL url = new URL("http://stackoverflow.com/");
    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));

    String s = null;
    while ((s = reader.readLine()) != null)
        System.out.println(s);
}
like image 199
Travis Gockel Avatar answered Sep 23 '22 03:09

Travis Gockel