I am using if-modified-since in the HTTP header to decide if I should download file.
App was tested and everything was OK, but now I am getting errors when I ask my NSHTTPURLResponse instance response.statusCode or [[response allHeaderFields] objectForKey:@"Last-Modified"].
It seems to be just NSURLResponse. What are the possible reasons?
I’ve readthis topic but the problem still is not clear for me.
Thanks in advance!
UPD:
some code:
NSURL *url = [NSURL URLWithString:urlString];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:url];
[myRequest setHTTPMethod:@"GET"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[myRequest addValue:[df stringFromDate:lastModifiedLocal] forHTTPHeaderField:@"If-Modified-Since"];
myRequest.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
NSHTTPURLResponse *response=nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error) {
NSLog(@"Error sending request: %@", [error localizedDescription]);
}
//As was advised
NSHTTPURLResponse* newResp = (NSHTTPURLResponse*)response;
//crash here
NSLog(@"%d", newResp.statusCode);
UPD:
Error in code – myRequest and request are different variables. Problem solved.
It is very likely that the request is failing and no response object is being generated. You can check this as follows:
As was suggested by another commenter, you should probably include an NSError object so you can check errors more effectively:
Then, you can check the error before checking the response: