views:

194

answers:

3

How can I programmatically tell if a binary file on a website (e.g. image) has changed without downloading it? Is there a way using HTTP methods (in C# in this case) to check prior to fully downloading it?

+4  A: 

You can do a HEAD request and check the last-modified datetime value, as well as the content-length.

RedFilter
Thanks for mentioning content-length: it's a simple check but a great fallback if the server is not setup right
Frank Krueger
+9  A: 

Really, you want to look for the Last-Modified header after issuing a HEAD request (rather than a GET). I wrote some code to get the HEAD via WebClient here.

Marc Gravell
It should be noted that not all sites provide a correct Last-Modified header, even though they should to have their site cached properly.
Ben S
But there is only so much you can do if the server doesn't play by the rules.
Bryan
If the server doesn't provide correct cache control headers (Last-Modified etc.) then there is no way to tell whether the file has changed since a particular time other than downloading it and comparing the contents.
David Zaslavsky
+6  A: 

You can check that whether the file is changed or not by requesting with HEAD.

Then, returned response header may include Last-Modified, or ETag if the web server support.

xrath
Agreed, ETags are the best solution to this problem where they are supported.
Doug McClean
Yes, that's the approach in a solution I did. i ETag exists use that, otherwise fall back to Last-Modified.
Michael Stum