0
         URLPing urlPing = new URLPing();
         PingResponse pingResponse = urlPing.ping(URLName);
         if(pingResponse.getResponseCode() == 200){
                response = true;
         }
         else{
                response=false;
         }

This is what i have tried at present.

Linda
  • 21
  • 1
  • 1
  • 2
  • 1
    First, post the code please. Second, you might not need ping/wget if you use URLConnection to connect to URLs. – Vineet Reynolds Jun 17 '11 at 04:25
  • 2
    Possible duplicate: http://stackoverflow.com/questions/3584210/preferred-java-way-to-ping-a-http-url-for-availability – Grega Kešpret Jun 17 '11 at 04:25
  • 1
    Possible duplicate of http://stackoverflow.com/questions/3584210/preferred-java-way-to-ping-a-http-url-for-availability – Andy Jun 17 '11 at 04:26
  • Possible duplicate of http://stackoverflow.com/questions/2799938/httpurlconnection-timeout-question – Babar Jun 17 '11 at 06:35

2 Answers2

4

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;
}
gtiwari333
  • 24,554
  • 15
  • 75
  • 102
0

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;
}
Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64