0

I'm using code that I got from a tutorial for finding the "Date Modified" info of a file on a server. It goes as follows...

// 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"];
}

And, for the most part, it works! Wa-hoo.

Except, for a few files, it seems to be returning outdated information. In fact, for at least one, it's returning a date (Fri, 24 Apr 2015 04:32:55 GMT) for a file that doesn't even exist anymore. I deleted it from the server, but it's still returning that value every time I run my app, as if the file still existed.

I've checked and re-checked that the remoteFileURL is pointing to the right URL, and I've confirmed that the file doesn't exist on the server anymore. And, like I said, for most files it works perfectly, so obviously the system isn't entirely broken.

This is my first experience with NSMutableURLRequest and NSHTTPURLResponse and getting file headers and such. Is there something I don't know, that would explain this? Could there be a cache or something that needs to be cleared?

And ideally, what can I do to ensure that the proper info is being received by my app?

Thanks in advance!

Nerrolken
  • 1,975
  • 3
  • 24
  • 53
  • 1
    Did you try it by removing cache information ?, Add this [[NSURLCache sharedURLCache] removeAllCachedResponses]; – Jageen Apr 27 '15 at 03:30
  • @Jageen I knew it was a caching thing!!! I was just expecting it to be a server-side solution. Yeah, I added that line at the very beginning, and it works great. Anyway, thanks for helping out the NSURL new guy. :) Add that as an answer, and I'll accept it. – Nerrolken Apr 27 '15 at 03:53

1 Answers1

1

Posting my comment as an answer

This type of thing happends because of caching mechanism of NSURL.
Try to add below line to remove all cached data.

[[NSURLCache sharedURLCache] removeAllCachedResponses];



If you want to ignore caching for particulate request try below code
NSURL *url = [NSURL URLWithString:aStrFinalURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
Jageen
  • 6,345
  • 2
  • 37
  • 56