If there is an XML file on a server. Is there a way to get it's version or last modified date without downloading the file and reading them from it?
Basically what I'm trying to do is to check if the file is a newer version than the one on my PC Downlo开发者_如何学Cad it, and if not, do not download it.
I now download it and check it's version. But I don't want to waste time downloading if it is the same version or has the same last modified date.
There are at least two ways to do this.
The first is to do a HEAD request on the file, rather than a GET. The HEAD request will return the file name, last modified date, content length, and a few other things. But it doesn't download the file. You can check the last modified date and download if it's newer than the one you have.
The other way to do it is to do a GET request and set the IfModifiedSince
property of the HttpWebRequest
object to the timestamp of the the file that you currently have. If the file on the server is newer, it will be downloaded. Otherwise you'll get a status code of 304 (Not Modified).
you could make a C# (or any language for that matter) Generic Handler (or script or page) that takes 1 parameter FileName (of the XML file) and writes the last modified date to the output. Then, if the date catches your fancy, you start your download
Here is an example for a Generic Handler (note that it probably still needs some error catching):
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string fileName = context.Request.QueryString.Get("fileName");
FileInfo fileInfo = new FileInfo(Server.MapPath(".") + "/" + fileName);
context.Response.Write(fileInfo.LastWriteTime.ToString());
}
There is a tecnique used in Ruby On Rails for achieve the optimization you are asking for.
You can edit the file name adding the timestamp of the last edit at the end of it xmlname_[TIMESTAMP].xml . When the client require the file if it has the same file name does not download it. If your application lives in a browser, when the client require the file, through a browser it will no download the new version that will be in the cache of the browser.
Hope it helps
If there is an XML file on a server. Is there a way to get it's version or last modified date without downloading the file and reading them from it?
Not as such…
Basically what I'm trying to do is to check if the file is a newer version than the one on my PC Download it, and if not, do not download it.
Sounds like you want standard HTTP cache control. The client could send an If-Modified-Since request or check ETags.
See the mnot caching tutorial for details.
Have you considered using ETag for caching mechanism? It is part of HTTP/1.1 specification and a strong ETag is probably what you are looking for (assuming you can process HTTP headers)
You could also make conditional query based on last known modified date using If-Modified-Since request header.
精彩评论