Is there something similar to WebClient.DownloadString in Java?

一世执手 提交于 2020-01-03 13:02:45

问题


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


回答1:


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);
}



回答2:


You can use the Java classes directly:

URL url = new URL("http://www.example.com");
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
...

but it's more recommended to use Apache HttpClient as HttpClient will handle a lot of things that you'll have to do yourself with the Java native classes.



来源:https://stackoverflow.com/questions/2098606/is-there-something-similar-to-webclient-downloadstring-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!