AFNetworking returns 200 when it should receive 304 - ETag

I am trying to use etag to cache my images, the first time it loads all the images, as if it repeated the second time that it had to enter a failed block with 304.

I tried to make a request from outside, and I get 304, as it should, I just have problems with AFNetworking.

NSString *urlString = [API_BASE_URL_PHOTOS stringByAppendingPathComponent:[photo getPathToPhoto]]; NSString *properlyEscapedURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:properlyEscapedURL]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; if ([UserDefaultManager getEtagForKey:photo.nom] != nil) { [request setValue:[UserDefaultManager getEtagForKey:photo.nom] forHTTPHeaderField:ETAG_IF_NONE_MATCH]; } [request setHTTPMethod:API_METHOD_GET]; AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { [FileUtils createDirectoryInDocumentsWithFileName:photo.folderName]; NSString *docPath = [FileUtils getPathInDocumentsWithFileName:[photo getPathToPhoto]]; // Save Image NSData *imageData = UIImageJPEGRepresentation(image, 90); [imageData writeToFile:docPath atomically:YES]; if ([response respondsToSelector:@selector(allHeaderFields)]) { NSDictionary *allHeaderFields = [response allHeaderFields]; [UserDefaultManager setEtag:[allHeaderFields objectForKey:ETAG] forKey:photo.nom]; } if ([[DownloadManager sharedManager].downloadQueue.operations count] == 0) { [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_DOWNLOAD_ALL_PHOTOS_FINISHED object:nil]; } } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { if (response.statusCode != RESPONSE_CODE_PHOTO_UP_TO_DATE) { LogDebug(@"%@", [error localizedDescription]); } }]; 
+4
source share
1 answer

I was able to fix this by changing my request to

  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60]; 

hope this helps

+6
source

Source: https://habr.com/ru/post/1495494/


All Articles