怎样用java获取URL返回状态码
1个回答
展开全部
// 用getResponseCode可以获取URL返回状态码
String surl = "";
try {
surl="你的url";
URL url = new URL(surl);
URLConnection rulConnection = url.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection;
httpUrlConnection.setConnectTimeout(300000);
httpUrlConnection.setReadTimeout(300000);
httpUrlConnection.connect();
String 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(getCurDateTime()+"连接"+surl+"正常");
}catch(Exception ex){
System.out.println(ex.getMessage());
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询