java检测当前URL是否可连接或是否有效
http://www.flypeng.com win10系统 发布时间:2015-09-16 09:56 来源:未知 浏览:加载中
import java.net.HttpURLConnection;
import java.net.URL;
 
public class URLAvailability {
private static URL url;
private static HttpURLConnection con;
private static int state = -1;
 
/**
* 功能:检测当前URL是否可连接或是否有效, 描述:最多连接网络 2 次, 如果 2 次都不成功,视为该地址不可用
* @param urlStr 指定URL网络地址
* @return URL
*/
public static synchronized boolean isConnect(String urlStr) {
int counts = 0;
if (urlStr == null || urlStr.length() <= 0) {
return false;
}
while (counts < 1) {
try {
url = new URL(urlStr);
con = (HttpURLConnection) url.openConnection();
state = con.getResponseCode();
System.out.println(counts + "= " + state);
if (state == 200) {
return true;
}
break;
} catch (Exception ex) {
counts++;
System.out.println("URL不可用,连接第 " + counts + " 次");
urlStr = null;
continue;
}
}
return false;
}
 
public static void main(String[] args) {
System.out.println(URLAvailability.isConnect("http://192.168.1.135:8000"));
System.out.println(URLAvailability.isConnect("http://192.168.1.131:8000"));
}
}

如果你有好的win10资讯或者win10教程,以及win10相关的问题想要获得win10系统下载的关注与报道。
欢迎加入发送邮件到657025171#qq.com(#替换为@)。期待你的好消息!