博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA訪问URL
阅读量:7037 次
发布时间:2019-06-28

本文共 1777 字,大约阅读时间需要 5 分钟。

JAVA訪问URL:

package Test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URI;import java.net.URISyntaxException;import java.net.URL;import java.net.URLConnection;import java.awt.Desktop;public class URLTest {    public static void main(String[] args) {		String urlStr = "http://www.baidu.com";  		URL url;		try {		    url = new URL(urlStr);		    URLConnection URLconnection = url.openConnection();  		    HttpURLConnection httpConnection = (HttpURLConnection)URLconnection;  		    int responseCode = httpConnection.getResponseCode();  		    if (responseCode == HttpURLConnection.HTTP_OK) {  		        System.err.println("成功");		        InputStream urlStream = httpConnection.getInputStream();  		        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlStream));  		        String sCurrentLine = "";  		        String sTotalString = "";  		        while ((sCurrentLine = bufferedReader.readLine()) != null) {  		            sTotalString += sCurrentLine;  		        }  		        System.err.println(sTotalString); 			        runBroswer(urlStr);		    }else{		        System.err.println("失败");		     }		} catch (Exception e) {		    e.printStackTrace();		}                    }        public static void runBroswer(String webSite) {        try {            Desktop desktop = Desktop.getDesktop();            if (desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {                URI uri = new URI(webSite);                desktop.browse(uri);            }        } catch (IOException ex) {            ex.printStackTrace();        } catch (URISyntaxException ex) {            ex.printStackTrace();        }    }}

转载地址:http://uvyal.baihongyu.com/

你可能感兴趣的文章
Git常用命令
查看>>
libevent学习
查看>>
动态代理的几种方式
查看>>
Collections常用方法总结
查看>>
微信小程序
查看>>
bash变量
查看>>
知识点049-supervisor
查看>>
干货满满,Android热修复方案介绍
查看>>
罗振宇跨年演讲之夜 阿里云护航得到App
查看>>
django中间键
查看>>
2017/09/22脚本练习
查看>>
Http post 常用的四种请求方式
查看>>
Python常用模块2
查看>>
直播APP开发如何得到用户青睐?
查看>>
Day26 正则介绍_grep工具
查看>>
数据库-删除数据
查看>>
753个大奖邀请各位新老博主入驻云栖,请技术人员广而告之
查看>>
apache域名重定向rewrite
查看>>
LNMP架构介绍,php安装
查看>>
Spring Boot教程(三十一)创建含有多module的springboot工程
查看>>