在java 中判断网站正确打开
2008-11-26 17:36
134 查看
这里是程序片断,
try{
URL url = new URL("http://www.mocoto.com/");
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
httpConnection.getURL();
int responseCode=httpConnection.getResponseCode();
String ResponseMessage=httpConnection.getResponseMessage();
System.out.println("^X^"+x+"/t"+httpConnection.getURL());
System.out.println("^X^"+x+"/t"+responseCode);
System.out.println("^X^"+x+"/t"+ResponseMessage);
}
catch(Exception e){}
-----------------------------
System.out.println("^X^"+x+"/t"+ResponseMessage);
输出为 ok ,就是连上了。对了,上面的程序中变量x是我循环时用的,你可以去掉
其中
int responseCode = httpConnection.getResponseCode();
这个方法返回了 一个 ResponseCode 值
这是一个 http 协议的 连接返回值 其中 200 表示正常连接
其他的 如 400 401 403 404 等等 都表示连接出了问题
每个 三位数表示一种错误 详情以去查 http 协议的规范
所以 基本上 只要判断 这个 ResponseCode 是否为 200 就可以了
try{
URL url = new URL("http://www.mocoto.com/");
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
httpConnection.getURL();
int responseCode=httpConnection.getResponseCode();
String ResponseMessage=httpConnection.getResponseMessage();
System.out.println("^X^"+x+"/t"+httpConnection.getURL());
System.out.println("^X^"+x+"/t"+responseCode);
System.out.println("^X^"+x+"/t"+ResponseMessage);
}
catch(Exception e){}
-----------------------------
System.out.println("^X^"+x+"/t"+ResponseMessage);
输出为 ok ,就是连上了。对了,上面的程序中变量x是我循环时用的,你可以去掉
其中
int responseCode = httpConnection.getResponseCode();
这个方法返回了 一个 ResponseCode 值
这是一个 http 协议的 连接返回值 其中 200 表示正常连接
其他的 如 400 401 403 404 等等 都表示连接出了问题
每个 三位数表示一种错误 详情以去查 http 协议的规范
所以 基本上 只要判断 这个 ResponseCode 是否为 200 就可以了
相关文章推荐
- 自动评分网站中判断正确多少题核心代码
- Java中用正则表达式判断日期格式是否正确
- [修改]脚本判断网站是否能打开
- Java - - 判断表达式中的括号是否正确配对
- php里判断一个网站和网页是否能打开
- 你在Java中用正则表达式判断日期字符串是否正确吗?
- 用java实现结婚、离婚小程序,其中有一些判断条件,正确掌握this关键字的用法
- java中判断输入日期的格式是否正确
- Java用正则表达式判断输入的电话号码格式是否正确
- php里判断一个网站和网页是否能打开
- java程序怎么打开一个网站的页面?
- java实现关机、重启、打开运用程序及网站
- windows 判断环境变量有无输入正确 echo %JAVA_HOME%
- 如何用.net程序来判断一个网站是否正常打开小解
- java判断身份证号是否正确
- java 判断格式是否正确
- Java正确判别出文件的字符集(尤其是UTF-8(无BOM)和GBK的判断)
- Java判断时间,Email,手机号是否为正确格式
- java后台怎么判断是手机还是电脑访问网站
- java Puzzles 之 如何正确的判断奇数