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());
          } 

解决方案 »

  1.   

    返回结果:
    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,怎么办,什么问题
      

  2.   

    200表示连接成功,你换个不存在的url试试
      

  3.   

    换了,http;//www.hao123.com我用的分号,no protocol: http;//www.hao123.com
      

  4.   

    还有,http://www.hao123.com/admin.php
    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;这不是我想要的
    应该怎么办??
      

  5.   

    我就想直接判断这个页面是不是存在,要是存在,返回200;不存在404;有错误500,就是返回http状态码,应该怎么写
      

  6.   

    HttpClient和UrlConnection
    会自动处理http 重定向,
    导致重定向时拿取得状态码是200,
    而无法正确的获取302//设置本次连接禁止重定向 
    httpUrlConnection.setInstanceFollowRedirects(false);
      

  7.   

    http://blog.csdn.net/caoyishuai/article/details/7937023