【发布时间】:2010-08-13 17:42:07
【问题描述】:
我有一个网络服务来下载文件。对于每个传入的请求,我都会检查请求下载的文件的校验和和时间戳,以及服务器上的文件。如果它们相同,我不必再次下载。
服务器端的代码是:
string checksum; //calculate this using methods in System.Security.Cryptography
string timestamp = File.GetLastAccessTimeUtc(filename).ToString();
string incCheckSum = WebOperationContext.Current.IncomingRequest.Header["If-None-Match"];
string incTimestamp = WebOperationContext.Current.IncomingRequest.Header["If-Modified-Since"];
if(checksum == incCheckSum && timestamp == incTimeStamp)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotModified;
return null;
}
WebOperationContext.Current.OutgoingResponse.Headers["Last-Modified"] = timestamp;
WebOperationContext.Current.OutgoingResponse.Headers["ETag"] = checksum;
return FileStream("Filename",FileMode.Open, FileAccess.Read,FileShare.Read);
在客户端:
HttpWebRequest request = (HttpWebRequest)WebRequest.create("http://somewebsite.com");
request.Header["If-None-Match"] = //get checksum file on the disk
request.Header["If-Modified-Since"] = "Last Modified Time" // I get an exception here:
异常说,
"必须使用 合适的属性”
那我做
request.IfModifiedSince = //Last Access UTC time of the file
现在,此更改会导致问题。每当请求到达服务器时,最后一次访问时间总是采用不同的格式,并且永远不会匹配。因此,如果文件的最后修改时间是 2010 年 8 月 13 日下午 5:27:12,在服务器端,我将 ["If-Modified-Since"] 值设为“Fri, 13 Aug 2010 17:27:12格林威治标准时间"
我该如何纠正这个问题?
当我使用提琴手并将以下内容添加到“请求标头”时:
If-Modified-Since= last access time
If-None-Match= checksum
这很好用。
【问题讨论】:
标签: c# http-headers