gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:30:10 26 4
gpt4 key购买 nike

我正在尝试使用 etag 来缓存我的图像,第一次它会像它应该的那样下载所有图像,但是第二次它也会这样做,因为它应该进入带有 304 的失败 block 。

我已经尝试在外部发出请求,但我得到了 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];

希望对你有帮助

关于ios - AFNetworking 在应该接收 304 时返回 200 - ETag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18076651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com