开发者

How we can Download a HTML Page using JAVA?

开发者 https://www.devze.com 2023-01-08 19:50 出处:网络
How we can Download a HTM开发者_JAVA百科L Page using JAVA??Here is the code: public static String savePage(final String URL) throws IOException {

How we can Download a HTM开发者_JAVA百科L Page using JAVA??


Here is the code:

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

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消