检测URL是否真实有效_JAVA_编程开发_程序员俱乐部

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

检测URL是否真实有效

 2011/9/27 7:35:14  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;}while(counts<5){try{HttpURLConnectionconnection=(HttpURLConnection
  • 标签:检测 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;??
??????? }??
??????? while (counts < 5) {??
??????????? try {??
??????????????? HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();??
??????????????? int state = connection.getResponseCode();??
??????????????? if (state == 200) {
??????????????????? 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/cozone/20/34/203441/images/148_60_1.gif"));?
??? }??
}?

发表评论
用户名: 匿名