package com.test;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
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) {
// 1.URL(String spec)
// 根据 String 表示形式创建 URL 对象。
// 2.URLConnection openConnection()
// 返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url)
.openConnection();
// connection对象 返回状态码。。。。
int state = connection.getResponseCode();
if (state == 200) {
return true;
}
break;// 状态码
} catch (Exception e) {
//URL 不合法一定报异常
//有异常的话 一定要抓出来 并试五次 次数加一counts++;
counts++;
continue;
}
}
return flag;
}
public static void main(String[] args) {
test2 check = new test2();
System.out.println(check.isConnect("http://www.qq.com/"));
}
}
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
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) {
// 1.URL(String spec)
// 根据 String 表示形式创建 URL 对象。
// 2.URLConnection openConnection()
// 返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url)
.openConnection();
// connection对象 返回状态码。。。。
int state = connection.getResponseCode();
if (state == 200) {
return true;
}
break;// 状态码
} catch (Exception e) {
//URL 不合法一定报异常
//有异常的话 一定要抓出来 并试五次 次数加一counts++;
counts++;
continue;
}
}
return flag;
}
public static void main(String[] args) {
test2 check = new test2();
System.out.println(check.isConnect("http://www.qq.com/"));
}
}