brainoverflow
brainoverflow

Reputation: 581

How to check the modify time of a remote file

I Am in need of knowing the last modification DateTime of a remote file prior to downloading the entire content. This to save up on downloading bytes I am never going to need anyway.

Currently I am using WebClient to download the file. It is not needed to keep the use of WebClient specifically. The Last-Modified key can be found within the response headers but the entire file is downloaded at that point in time.

WebClient           webClient        = new WebClient();
byte[]              buffer           = webClient.DownloadData( uri );
WebHeaderCollection webClientHeaders = webClient.ResponseHeaders;
String              modified         = webClientHeaders.GetKey( "Last-Modified" );

Also I am not sure if that key is always included at each file on the internet.

Upvotes: 2

Views: 1738

Answers (1)

Krumelur
Krumelur

Reputation: 32497

You can use the HTTP "HEAD" method to just get the file's headers.

...
var request = WebRequest.Create(uri);
request.Method = "HEAD";
...

Then you can extract the last modified date and check whether to download the file or not.

Just be aware that not all servers implement Last-modified properly.

Upvotes: 4

Related Questions