开发者

Ping url and get status in java

         URLPing urlPing = new URLPing();
         PingResponse pingR开发者_开发知识库esponse = urlPing.ping(URLName);
         if(pingResponse.getResponseCode() == 200){
                response = true;
         }
         else{
                response=false;
         }

This is what i have tried at present.


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


To ping a 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜