测试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/"));??
??? }??
}?