解决方案 »

  1.   

    import java.io.FilterInputStream;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    public class CountNet { 
        public static void main(String[] args) {
                int time = 0;
                while (true) {
                   if (getCon() == 0) {
                       time++;
                   }
                    try {    
                    Thread.sleep(1000);
                    } catch (InterruptedException e) {
                          e.printStackTrace(); 
                    } 
                    System.out.println("已经连接上了" + time + "秒");
                } 
        } 
        public static int getCon() { 
              URL url = null;  // 构造一个下载的URL路径 
              try {   
                  url = new URL("http://www.baidu.com");   // 一个输入流  
                  FilterInputStream in;   
                  in = (FilterInputStream) url.openStream();   // 关闭流对象   
                  in.close();  
              } catch (MalformedURLException e) {   
                  return -1;  
              } catch (IOException e) {   
                  e.printStackTrace();  
              }  return 0; 
        }
    }