AFNetworking在应接收304-ETag时返回200



我正在字符串中使用etag来缓存我的图像,第一次它像应该的那样下载所有图像,但第二次它也这样做了,因为它应该进入304的失败块。

我试着从外部提出请求,我得到了304,就像我应该得到的一样,只是在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]);
    }
}];

我设法通过将请求更改为来解决此问题

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

希望这能帮助

最新更新