【问题标题】:Ping url and get status in javaping url并在java中获取状态
【发布时间】:2011-09-16 21:37:48
【问题描述】:
         URLPing urlPing = new URLPing();
         PingResponse pingResponse = urlPing.ping(URLName);
         if(pingResponse.getResponseCode() == 200){
                response = true;
         }
         else{
                response=false;
         }

这是我目前尝试过的。

【问题讨论】:

标签: java


【解决方案1】:

Ping URL from JAVA Code

public static boolean pingUrl(final String address) {
 try {
  final URL url = new URL("http://" + address);
  final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
  urlConn.setConnectTimeout(1000 * 10); // mTimeout is in seconds
  final long startTime = System.currentTimeMillis();
  urlConn.connect();
  final long endTime = System.currentTimeMillis();
  if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
   System.out.println("Time (ms) : " + (endTime - startTime));
   System.out.println("Ping to "+address +" was success");
   return true;
  }
 } catch (final MalformedURLException e1) {
  e1.printStackTrace();
 } catch (final IOException e) {
  e.printStackTrace();
 }
 return false;
}

【讨论】:

    【解决方案2】:

    要 ping 一个 URL:

    public static boolean exists()
    {
       try
       {
          return (Runtime.getRuntime().exec("/system/bin/ping -c 1 google.com").waitFor() == 0);
       }
       catch (IOException | InterruptedException exception)
       {
          exception.printStackTrace();
          // Handler
       }
    
       return false;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      相关资源
      最近更新 更多