import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by matt0614@qq.com on 2017/11/3 0003.
*/
public class UrlConnectUtil {
public static int testWsdlConnection(String address) throws Exception {
int status = 404;
try {
String url = toUtf8String(address);
URL urlObj = new URL(url);
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;
}
public static String toUtf8String(String url){
StringBuffer sb = new StringBuffer();
for (int i = 0; i < url.length(); i++) {
char c = url.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = String.valueOf(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0)
k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
}
状态码查询相关文章:
http://www.cnblogs.com/lianghe01/p/4757438.html