Search code examples
ioshttp-headersnsurlconnectionnshttpurlresponse

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


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!


Solution

  • 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];