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?
Asked
Active
Viewed 931 times
5
-
I don't think so; if you request an actual file, you just get an octet stream. AFAIK there's no calls in the spec that allow you to interrogate file attributes the way you'd need to, but I'm curious to see if there's a way... – Joe Sep 25 '09 at 18:09
3 Answers
9
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.

Community
- 1
- 1

Marc Gravell
- 1,026,079
- 266
- 2,566
- 2,900
-
1It 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 Sep 25 '09 at 18:14
-
But there is only so much you can do if the server doesn't play by the rules. – Bryan Sep 25 '09 at 18:18
-
1If 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 Z Sep 25 '09 at 18:53
6
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
- 834
- 6
- 14
-
Agreed, ETags are the best solution to this problem where they are supported. – Doug McClean Sep 25 '09 at 18:25
-
Yes, that's the approach in a solution I did. i ETag exists use that, otherwise fall back to Last-Modified. – Michael Stum Sep 25 '09 at 22:38
5
You can do a HEAD request and check the last-modified datetime value, as well as the content-length.

D'Arcy Rittich
- 167,292
- 40
- 290
- 283
-
Thanks for mentioning content-length: it's a simple check but a great fallback if the server is not setup right – Frank Krueger Sep 25 '09 at 22:49