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"));
    }
}
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注