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