5

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?

Alfred Myers
  • 6,384
  • 1
  • 40
  • 68
Greg
  • 34,042
  • 79
  • 253
  • 454
  • 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 Answers3

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
  • 1
    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 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
  • 1
    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 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
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