package com.wonders.commonweb.core.utils;
import com.expansion.postUtils.PostUtils;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
public class postUtils {
public static String getUrlCode(String surl){
String code=null;
try {
URL url = new URL(surl);
URLConnection rulConnection = url.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection;
httpUrlConnection.setConnectTimeout(300000);
httpUrlConnection.setReadTimeout(300000);
httpUrlConnection.connect();
code = new Integer(httpUrlConnection.getResponseCode()).toString();
String message = httpUrlConnection.getResponseMessage();
System.out.println("getResponseCode code ="+ code);
System.out.println("getResponseMessage message ="+ message);
if(!code.startsWith("2")){
throw new Exception("ResponseCode is not begin with 2,code="+code);
}
System.out.println("连接"+surl+"正常");
}catch(Exception ex){
System.out.println(ex.getMessage());
}
return code;
}
public static void main(String[] args) {
String surl="https://www.baidu.com/";
System.out.println(getUrlCode(surl));
}
}
结果截图: