• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

How to create a java application that monitors a website

Hi, all

I need to create a java application that can monitor a website. If the website is down, it will send emails to some persons. Could you please help me to do it? Thanks.
0
CodeLover
Asked:
CodeLover
1 Solution
 
CEHJCommented:
Open a URLConnection on its index. If you get an Exception - send email.
0
 
vfwoodCommented:
import java.io.*;
import java.net.*;

public class HttpMonitor extends Thread {
     // how long to wait between attempts..
     private final static int SECONDS_TO_WAIT = 4;

     // url we wish to monitor.
     private final static String urlToMonitor = "http://www.google.com/";

     // entry point into application
     public static void main (String[] args)     {
          new HttpMonitor().start();
     }

     // this is called by start() in main() method
     public void run()     {
          boolean running = true;
          while (running)     {
               try     {
                    // causes current thread to sleep x milliseconds
                    sleep (SECONDS_TO_WAIT * 1000);

                    URL myurl = new URL(urlToMonitor);
                    URLConnection conn = myurl.openConnection();
                    BufferedReader br = new BufferedReader(
                              new InputStreamReader(
                                   conn.getInputStream()));
                    // we will hold webpage contents here
                    String buff = "";
                    // and store each line in this temporary var
                    String tmp = "";
                    // keep reading till returns null
                    while ((tmp = br.readLine()) != null) {
                         buff+= "\n" + tmp;
                    }
                    // at this point we have read the webpage into String buff
                    // perform any checks you want to here...
                    System.out.println(new java.util.Date() + "> read ok");
               } catch (Exception e)     {
                    // handle your own exceptions here...
                    System.out.println(new java.util.Date() + ">" + e);

               }
          }
     }
}

comments should help
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now