import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.HttpURLConnection; public class WebPageAccess { public static void main(String[] args) { String url = "http://example.com"; // 输入要访问的网页链接 try { // 创建URL对象 URL link = new URL(url); // 创建HttpURLConnection对象 HttpURLConnection connection = (HttpURLConnection) link.openConnection(); // 设置请求方法为GET connection.setRequestMethod("GET"); // 发送GET请求 int responseCode = connection.getResponseCode(); // 判断请求是否成功 if (responseCode == HttpURLConnection.HTTP_OK) { // 读取返回的内容 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuffer response = new StringBuffer(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); // 打印返回的内容 System.out.println(response.toString()); } else { System.out.println("请求失败,响应代码:" + responseCode); } // 关闭连接 connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }到此这篇可以直接进入的网站的代码(可以直接进入的网站的代码html)的文章就 介绍到这了,更多相关内容请继续浏览下面的相关 推荐文章,希望大家都能在编程的领域有一番成就!
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/qdhtml/81521.html