Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中是否有类似于WebClient.DownloadString的东西?_Java_Parsing_Webclient - Fatal编程技术网

Java中是否有类似于WebClient.DownloadString的东西?

Java中是否有类似于WebClient.DownloadString的东西?,java,parsing,webclient,Java,Parsing,Webclient,我想下载一个网站的html源代码来解析一些信息。如何在Java中实现这一点?您可以直接使用Java类: URL url = new URL("http://www.example.com"); URLConnection conn = url.openConnection(); InputStream in = conn.getInputStream(); ... 但更推荐使用它,因为HttpClient将处理许多您必须自己处理的Java本机类。只需从openStream()返回的URL In

我想下载一个网站的html源代码来解析一些信息。如何在Java中实现这一点?

您可以直接使用Java类:

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

但更推荐使用它,因为HttpClient将处理许多您必须自己处理的Java本机类。

只需从openStream()返回的URL InputStream中附加一个BufferedReader(或任何读取字符串的内容)


谢谢,别忘了关闭阅读器
try(BufferedReader阅读器…){}
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);
}