[英]How to control when a remote iCal (.ics) file has been updated and the calendar should sync?
[英]How can I see if a file on a remote webserver has been updated?
公司发布文件并定期对其进行更新。
例如。 http://somecompany.com/blah/file.zip
我想每天轮询一次,看看是否已更新。
我猜我可以查看http标头以获取文件大小,或者可以查看表明其已更改的日期/校验和。
我想知道在C#中是否有一个优雅的解决方案,除了创建直接TCP连接并发送一些命令之外,还可以。
有任何想法吗?
由于文件位于远程Web服务器上,因此您可以使用Last-Modified
标头字段来确定文件是否有任何更改。
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);
确保您使用HEAD
作为请求方法; 由于GET
会下载文件。
在PCL中,您无权访问完整.Net中的相同对象。 因此,这里有一些更灵活的代码:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.