Sandy D.
Sandy D.

Reputation: 3146

iOS: Send If Modified Since header with request

Does anyone know how to send If Modified Since header with a url request?

I'm trying to get a status code back that will tell me if an RSS page has been updated or not.

UPDATE #1: I was able to do a test doing the following:

NSURL *url = [NSURL URLWithString:self.url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:@"Tue, 18 Nov 2014 20:46:46 GMT" forHTTPHeaderField:@"If-Modified-Since"];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

    int responseCode = (int)[httpResponse statusCode];
    NSLog(@"All headers: %@", [httpResponse allHeaderFields]);
    NSLog(@"Status code:: %d", responseCode);
}];

What's weird though, if I delete my entire iOS application and run it I get the 304 status code. But, if I comment out the line where I add the If-Modified-Since value and re-run it -- then I get the 200 but when I uncomment that line and re-run it -- then the 304 error doesn't come back... It's a 200 error code. The only way to get the 304 error back is by deleting the entire application off my iPhone device.

Any ideas what's going on?

Upvotes: 4

Views: 2973

Answers (2)

gfly
gfly

Reputation: 1588

304 is not an error!It means the resource is same with your last request. Set the value of this to the Last-Modified value received from the last request to the same endpoint. If the resource has not been updated since the last request, the server will send a 304 (not modified) status code response.

Upvotes: 0

dminones
dminones

Reputation: 2306

A little late with this but since NSURLConnection is caching the responses in order to get the 304 code you should create your request like this.

NSURLRequest *request = [NSURLRequest requestWithURL:url
                                         cachePolicy:NSURLRequestReloadIgnoringCacheData
                                     timeoutInterval:60.0];

Upvotes: 11

Related Questions