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?

1
  • 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... Commented Sep 25, 2009 at 18:09

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It should be noted that not all sites provide a correct Last-Modified header, even though they should to have their site cached properly.
But there is only so much you can do if the server doesn't play by the rules.
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.
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.

2 Comments

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

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

1 Comment

Thanks for mentioning content-length: it's a simple check but a great fallback if the server is not setup right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.