3

Hi I know that on Firefox there is a website checker extension that will notify through Firefox whether a website has been updated.

Are there any code snippets for doing the same functionality? I'd like to have email notification for website update instead.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Dancyliu
  • 31
  • 1
  • 2

2 Answers2

2

This does the job persisting the last sha2 hash for the page contents and comparing the current hash against the persisted one every 5 seconds. By the way the exmaple relies on apache codec library for the sha2 operation.

import org.apache.commons.codec.digest.*;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * User: jhe
 */
public class UrlUpdatedChecker {

    static Map<String, String> checkSumDB = new HashMap<String, String>();

    public static void main(String[] args) throws IOException, InterruptedException {

        while (true) {
            String url = "http://www.stackoverflow.com";

            // query last checksum from map
            String lastChecksum = checkSumDB.get(url);

            // get current checksum using static utility method
            String currentChecksum = getChecksumForURL(url);

            if (currentChecksum.equals(lastChecksum)) {
                System.out.println("it haven't been updated");
            } else {
                // persist this checksum to map
                checkSumDB.put(url, currentChecksum);
                System.out.println("something in the content have changed...");

                // send email you can check: http://www.javacommerce.com/displaypage.jsp?name=javamail.sql&id=18274
            }

            Thread.sleep(5000);
        }
    }

    private static String getChecksumForURL(String spec) throws IOException {
        URL u = new URL(spec);
        HttpURLConnection huc = (HttpURLConnection) u.openConnection();
        huc.setRequestMethod("GET");
        huc.setDoOutput(true);
        huc.connect();
        return DigestUtils.sha256Hex(huc.getInputStream()); 
    }
}
Jaime Hablutzel
  • 6,117
  • 5
  • 40
  • 57
0

Use HttpUrlConnection by calling openConnection() on your URL object.

getResponseCode() will give you the HTTP response once you've read from the connection.

e.g.

   URL u = new URL ( "http://www.example.com/" ); 
   HttpURLConnection huc =  ( HttpURLConnection )  u.openConnection (); 
   huc.setRequestMethod ("GET"); 
   huc.connect () ; 
   OutputStream os = huc.getOutputStream (  ) ; 
   int code = huc.getResponseCode (  ) ;

(not tested!)