try {
// surl="http://www.baidu.com";
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());
}
http://www.sina.com
getResponseCode code =200
getResponseMessage message =OK
Wed May 30 02:13:42 GMT 2012连接http://www.sina.com正常
http;//www.hao123.com
http;//www.hao123.comno protocol: http;//www.hao123.com这里没有404,怎么办,什么问题
getResponseCode code =200
getResponseMessage message =OK
Wed May 30 02:56:52 GMT 2012连接http://www.hao123.com/admin.php正常
这里实际的效果是这个页面不存在,进的是一个error页面;但是他会自动跳转到http://www.hao123.com
结果返回还是200,ok;这不是我想要的
应该怎么办??
会自动处理http 重定向,
导致重定向时拿取得状态码是200,
而无法正确的获取302//设置本次连接禁止重定向
httpUrlConnection.setInstanceFollowRedirects(false);