测试URL是否可用_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 测试URL是否可用

测试URL是否可用

 2011/12/14 10:08:00  guolang2116  http://guolang2116.iteye.com  我要评论(0)
  • 摘要:importjava.net.URL;importjava.net.HttpURLConnection;publicclasstest2{privatebooleanisConnect(Stringurl){booleanflag=false;intcounts=0;if(url==null||url.length()<=0){returnflag;}//测试5次while(counts<5){try{HttpURLConnectionconnection=
  • 标签:测试 URL

import java.net.URL;??
import java.net.HttpURLConnection;??
?
public class test2 {??
??? private boolean isConnect(String url) {??
??????? boolean flag = false;??
??????? int counts = 0;??
??????? if (url == null || url.length() <= 0) {??
??????????? return flag;??
??????? }??

??????? //测试5次
??????? while (counts < 5) {??
??????????? try {??
??????????????? HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();??
??????????????? int state = connection.getResponseCode();??
??????????????? if (state == 200) {? //200:可用,401不可用
??????????????????? flag = true;??
??????????????? }??
??????????????? break;??
??????????? } catch (Exception ex) {??
??????????????? counts++;??
??????????????? continue;??
??????????? }??
??????? }??
??????? return flag;??
??? }??
?
??? public static void main(String[] args) {??
??? ??? test2 check = new test2();??
??? ??? System.out.println(check.isConnect("http://www.baidu.com/"));??
??? }??
}?

发表评论
用户名: 匿名