4

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?

Sandy D.
  • 3,166
  • 1
  • 20
  • 31
  • Check the answer same as here http://stackoverflow.com/questions/31551964/how-to-use-nsurlsession-to-determine-if-resource-has-changed/31567943#31567943 – mkumar Jul 23 '15 at 10:51
  • same post as http://stackoverflow.com/questions/31551964/how-to-use-nsurlsession-to-determine-if-resource-has-changed/31567943#31567943 – mkumar Jul 23 '15 at 10:52

2 Answers2

11

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];
dminones
  • 2,236
  • 20
  • 21
0

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.

gfly
  • 1,508
  • 1
  • 9
  • 5