From 713331cbd9e246da169739788b180fccdd421b34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=85=B6=E7=82=9C?= <10602846+zhang-qiweii@user.noreply.gitee.com> Date: Mon, 18 Sep 2023 15:43:45 +0000 Subject: [PATCH] =?UTF-8?q?20210340643=E5=BC=A0=E5=85=B6=E7=82=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张其炜 <10602846+zhang-qiweii@user.noreply.gitee.com> --- ...3\345\274\240\345\205\266\347\202\234.txt" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "20210340643\345\274\240\345\205\266\347\202\234.txt" diff --git "a/20210340643\345\274\240\345\205\266\347\202\234.txt" "b/20210340643\345\274\240\345\205\266\347\202\234.txt" new file mode 100644 index 0000000..c756ed7 --- /dev/null +++ "b/20210340643\345\274\240\345\205\266\347\202\234.txt" @@ -0,0 +1,48 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class WebCrawler { + + private static final String USER_AGENT = "Mozilla/5.0"; + + public static void main(String[] args) { + String url = "https://example.com"; // 指定要爬取的网站 URL + + try { + // 创建 URL 对象 + URL obj = new URL(url); + + // 创建 HttpURLConnection 对象 + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + + // 设置请求方法为 GET + con.setRequestMethod("GET"); + + // 设置请求头信息 + con.setRequestProperty("User-Agent", USER_AGENT); + + // 获取响应状态码 + int responseCode = con.getResponseCode(); + + // 打印响应状态码 + System.out.println("Response Code: " + responseCode); + + // 读取响应内容 + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuilder content = new StringBuilder(); + + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + in.close(); + + // 打印响应内容 + System.out.println("Response Content: " + content.toString()); + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file -- Gitee