From bff96af138eeeeb8eef98f4df7835662a06c79f6 Mon Sep 17 00:00:00 2001 From: zhangsiyuan <3837703520@qq.com> Date: Sun, 29 Mar 2026 19:55:50 +0800 Subject: [PATCH] =?UTF-8?q?w4-=E5=BC=A0=E6=80=9D=E6=B8=8A-202401070104?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project/src/project/utils/HttpUtils.java | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 project/src/project/utils/HttpUtils.java diff --git a/project/src/project/utils/HttpUtils.java b/project/src/project/utils/HttpUtils.java new file mode 100644 index 0000000..babcc52 --- /dev/null +++ b/project/src/project/utils/HttpUtils.java @@ -0,0 +1,30 @@ +package project.utils; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class HttpUtils { + public static String getHtml(String url) throws Exception { + URL obj = new URL(url); + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + con.setRequestMethod("GET"); + con.setRequestProperty("User-Agent", "Mozilla/5.0"); + + int responseCode = con.getResponseCode(); + if (responseCode != HttpURLConnection.HTTP_OK) { + throw new Exception("HTTP error code: " + responseCode); + } + + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); + String inputLine; + StringBuilder html = new StringBuilder(); + + while ((inputLine = in.readLine()) != null) { + html.append(inputLine); + } + in.close(); + return html.toString(); + } +} \ No newline at end of file