Java学习之路-NET-通过链接获取网页源码

使用URL类,获取网页源码
package cn.antraces.GUI.NET;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class ReadWeb {

    public static String read(String strUrl) throws IOException {
        URL url;
        BufferedReader bufferedReader;

        url = new URL(strUrl);
        bufferedReader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
        String s = "";

        StringBuffer stringBuffer = new StringBuffer("");

        while ((s = bufferedReader.readLine()) != null) {
            stringBuffer.append(s + "\r\n");
        }
        bufferedReader.close();

        return stringBuffer.toString();
    }

    public static void main(String[] args) throws IOException {
        System.out.println(read("https://i.loli.link/my-java-road-14-2"));
    }
}
按讚

發佈留言

電子郵件地址不會被公開。必填項已用 * 標註