4

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.

Community
  • 1
  • 1
Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
  • What is the result for `data` after the `sendSynchronousRequest:returningResponse:error:`? Also, Why not try passing in an actual `NSError` pointer so if something goes wrong you might possibly know what it is, by logging `error.localizedDescription`? – NJones Jan 17 '12 at 13:58
  • Data is normal, just as I expect. – Pavel Oganesyan Jan 18 '12 at 07:39
  • There was a mistake because of my stupid unconcentration - `[myRequest addValue:[df stringFromDate:lastModifiedLocal] forHTTPHeaderField:@"If-Modified-Since"]; myRequest.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;` and `NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];` after it, another variable with local URL, so no HTTP there. Sorry. Will approve most complete answer. – Pavel Oganesyan Jan 19 '12 at 09:22

2 Answers2

3

It is very likely that the request is failing and no response object is being generated. You can check this as follows:

if (response) {
    NSHTTPURLResponse* newResp = (NSHTTPURLResponse*)response;
    NSLog(@"%d", newResp.statusCode);
}
else {
    NSLog(@"No response received");
}

As was suggested by another commenter, you should probably include an NSError object so you can check errors more effectively:

NSHTTPURLResponse *response=nil;
NSError *error = nil;  
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: &error];  

Then, you can check the error before checking the response:

if (error) {
    NSLog(@"Error sending request: %@", [error localizedDescription]);
}
Tim Dean
  • 8,253
  • 2
  • 32
  • 59
0

You can simply cast it:

- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)aResponse 
{
    self.response = (NSHTTPURLResponse *)aResponse;
}

Edit

Your response is NULL. Try this:

NSHTTPURLResponse *response = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];  
hwaxxer
  • 3,363
  • 1
  • 22
  • 39