我正在获取一个不存在的文件的 header 信息?

I'm getting header info for a file that doesn't exist?

我正在使用从教程中获得的代码来查找服务器上文件的 "Date Modified" 信息。它是这样的...

// create a HTTP request to get the file information from the web server
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:remoteFileURL];
[request setHTTPMethod:@"HEAD"];

NSHTTPURLResponse* response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

// get the last modified info from the HTTP header
NSString* httpLastModified = nil;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
    httpLastModified = [[response allHeaderFields] objectForKey:@"Last-Modified"];
}

而且,在大多数情况下,它是有效的! Wa-hoo.

除了一些文件,它似乎返回了过时的信息。事实上,至少对于一个文件,它返回 甚至不再存在 的文件的日期 (Fri, 24 Apr 2015 04:32:55 GMT)。我从服务器上删除了它,但每次我 运行 我的应用程序时它仍然返回那个值,就好像文件仍然存在一样。

我已经检查 re-checked remoteFileURL 指向右侧 URL,并且我已经确认服务器上不再存在该文件。而且,就像我说的,对于大多数文件它都能完美运行,所以显然系统并没有完全损坏。

这是我第一次使用 NSMutableURLRequestNSHTTPURLResponse 以及获取文件 headers 等。有什么我不知道的,可以解释这个吗?会不会有缓存什么的需要清空?

理想情况下,我该怎么做才能确保我的应用收到正确的信息?

提前致谢!

发布我的评论作为答案

之所以会出现这种情况,是因为NSURL的缓存机制。
尝试添加以下行以删除所有缓存数据。

[[NSURLCache sharedURLCache] removeAllCachedResponses];



如果您想忽略微粒请求的缓存,请尝试以下代码

NSURL *url = [NSURL URLWithString:aStrFinalURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];