Pavel Oganesyan
Pavel Oganesyan

Reputation: 6924

NSHTTPURLResponse becomes NSURLResponse

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.

Upvotes: 4

Views: 7698

Answers (2)

Tim Dean
Tim Dean

Reputation: 8292

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]);
}

Upvotes: 3

hwaxxer
hwaxxer

Reputation: 3383

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

Upvotes: 0

Related Questions