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?
views:
194answers:
3
+4
A:
You can do a HEAD request and check the last-modified datetime value, as well as the content-length.
RedFilter
2009-09-25 18:08:38
Thanks for mentioning content-length: it's a simple check but a great fallback if the server is not setup right
Frank Krueger
2009-09-25 22:49:26
+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
2009-09-25 18:08:39
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
2009-09-25 18:14:56
But there is only so much you can do if the server doesn't play by the rules.
Bryan
2009-09-25 18:18:38
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
2009-09-25 18:53:46
+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
2009-09-25 18:15:41
Agreed, ETags are the best solution to this problem where they are supported.
Doug McClean
2009-09-25 18:25:13
Yes, that's the approach in a solution I did. i ETag exists use that, otherwise fall back to Last-Modified.
Michael Stum
2009-09-25 22:38:38