我正在使用etag緩存我的圖像,它第一次下載所有圖像時應該如此,但是第二次它應該進入失敗的塊時與304AFNetworking在應該接收304時返回200 - ETag
我試着提出請求外,我得到304像我應該,它只是與我有麻煩
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]);
}
}];
如果您在外部提出請求並獲得304,並且您在AFNetworking中創建並獲取200,那麼您可能會以不同方式提出請求。 AFNetworking不包含任何將更改服務器返回的HTTP狀態代碼的代碼。 –
是的,我設法算起來,我改變請求到 NSMutableURLRequest *請求= [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] 的CachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10]; 並且做了竅門 –