51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

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



`}
`

赞(1)
未经允许不得转载:工具盒子 » Java学习之路-NET-通过链接获取网页源码