判断网络链接是否正常(简单版)

//仅仅判断http返回码是否是200

//存在一个缺陷,如果网页被修复,它依然会返回200

try{   
  URL url_conn = new URL(urlStr);
  HttpsURLConnection http = (HttpsURLConnection) url_conn.openConnection();
  System.out.println(http);
  //System.out.println(bool_conn + "initialize");
  System.out.println(http.getResponseCode());
  if ("ok".equalsIgnoreCase(http.getResponseMessage())) {
   bool_conn = true;
   System.out.println("the net is ok");
   }
  else{
  // bool_conn = false;
   System.out.println("the net is bad");
   }

  }
  catch(IOException e){
   bool_conn = false;
   e.printStackTrace();
   //System.out.println("wrong");
   System.out.println(e.getStackTrace());
  }

你可能感兴趣的:(网络编程,.net)