Flask接口签名sign原理与实例代码浅析
304
2023-07-09
Java 测试URL地址是否能正常连接的代码
java 测试URL地址是否能正常连接
public static int testWsdlConnection(String address) throws Exception {
int status = 404;
try {
URL urlObj = new URL(address);
HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
oc.setUseCaches(false);
oc.setConnectTimeout(3000); // 设置超时时间
status = oc.getResponseCode();// 请求状态
if (200 == status) {
// 200是请求地址顺利连通。。
return status;
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return status;
}
定义了一个int型,如果返回可200则是地址能http://成功连通,如果返回0或者其他则是失败。
下面再看一段关于Java检测URL是否可用或者可打开的代码
package test;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.log4j.Logger;
public class CheskUrl {
private static Logger logger = Logger.getLogger(CheskUrl.class);
private static URL urlStr;
private static HttpURLConnection connection;
private static int state = -1;
private static String succ;
public synchronized String isConnect(String url) {
int counts = 0;
succ = null;
if (url == null || url.length() <= 0) {
return succ;
}
while (counts < 5) {
try {
urlStr = new URL(url);
connection = (HttpURLConnection) urlStr.openConnection();
state = connection.getResponseCode();
if (state == 200) {
succ = connection.getURL().toString();
}
break;
} catch (Exception ex) {
counts++; logger.info("loop :" + counts);
continue;
}
}
return succ;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~