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