Since you are extracting an XML file from a web server, this should be relatively easy without having to do the MD5 sum on the server side.
If you are making an HTTP request for an xml file, you can simply execute the HEAD request from the web server, and it will return if the file has changed or changed, or if it does not exist. It is also light weight, and the best part is that the server should already do this for you.
Edit : re-read your question, it looks like you had the same idea. Here is the code.
import java.net.*; import java.io.*; // Using HTTP_NOT_MODIFIED public static boolean Changed(String url){ try { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setRequestMethod("HEAD"); return (con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED); } catch (Exception e) { e.printStackTrace(); return false; } } // GET THE LAST MODIFIED TIME public static long LastModified(String url) { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); long date = con.getLastModified(); if (date == 0) System.out.println("No last-modified information."); else System.out.println("Last-Modified: " + new Date(date)); return date; }
See:
Alternatively, if your server supports them, you can use ETags to find out if your file has been modified.
Justin shield
source share