2025年可以直接进入的网站的代码(可以直接进入的网站的代码html)

可以直接进入的网站的代码(可以直接进入的网站的代码html)import java io BufferedRead import java io InputStreamR import java net URL import java net HttpURLConne public class WebPageAcces

大家好,我是讯享网,很高兴认识大家。



import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.HttpURLConnection; public class WebPageAccess {
讯享网
讯享网</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { String url </span>= "http://example.com"; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 输入要访问的网页链接</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建URL对象</span> URL link = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> URL(url); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建HttpURLConnection对象</span> HttpURLConnection connection =<span style="color: rgba(0, 0, 0, 1)"> (HttpURLConnection) link.openConnection(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置请求方法为GET</span> connection.setRequestMethod("GET"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发送GET请求</span> <span style="color: rgba(0, 0, 255, 1)">int</span> responseCode =<span style="color: rgba(0, 0, 0, 1)"> connection.getResponseCode(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断请求是否成功</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (responseCode ==<span style="color: rgba(0, 0, 0, 1)"> HttpURLConnection.HTTP_OK) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 读取返回的内容</span> BufferedReader reader = <span style="color: rgba(0, 0, 255, 1)">new</span> BufferedReader(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InputStreamReader(connection.getInputStream())); String line; StringBuffer response </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringBuffer(); </span><span style="color: rgba(0, 0, 255, 1)">while</span> ((line = reader.readLine()) != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { response.append(line); } reader.close(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印返回的内容</span> 
System.out.println(response.toString());
 } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { System.out.println(</span>"请求失败,响应代码:" +<span style="color: rgba(0, 0, 0, 1)"> responseCode); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 关闭连接</span> 
connection.disconnect();
讯享网 } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } } 
}

讯享网

小讯
上一篇 2025-04-28 13:39
下一篇 2025-06-07 16:01

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/159028.html