NibblyPig
NibblyPig

Reputation: 52942

How can I see if a file on a remote webserver has been updated?

A company publishes a file and updates it periodically.

Eg. http://somecompany.com/blah/file.zip

I'd like to poll once a day to see if it has been updated.

I'm guessing I can view the http headers to get the file size, or possibly a date/checksum that would indicate it had changed.

I'm wondering if there's an elegant solution to doing this in C# other than creating a direct TCP connection and sending some commands.

Any ideas?

Upvotes: 1

Views: 980

Answers (2)

JKennedy
JKennedy

Reputation: 18799

In a PCL you don't have access to the same objects are in the full .Net. So here is a more flexible bit of code:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://somecompany.com/blah/file.zip");
request.Method = "HEAD"; // Important - Not interested in file contents
string lastModified = null;
ManualResetEvent mre = new ManualResetEvent(false);
var req = request.BeginGetResponse((result) =>
{
    var resp = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
    lastModified = resp.Headers["Last-Modified"];
    mre.Set();
}, request);
mre.WaitOne();

Upvotes: 0

Kami
Kami

Reputation: 19407

As the file is on a remote web server you can use the Last-Modified header field to identify if there are any changes to the file.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://somecompany.com/blah/file.zip");
request.Method = "HEAD"; // Important - Not interested in file contents

HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
Console.WriteLine(resp.LastModified);

Ensure you are using HEAD as the request method; As GET would download the file.

Upvotes: 5

Related Questions