我们如何使用JAVA下载HTML页面?

4 html java networking

我们如何使用JAVA下载HTML页面?

Kla*_*ark 10

这是代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以在while循环中一个接一个地处理一行.