我正在尝试在服务器上做一个请求,它会响应一个Etag,用于缓存目的。我已经为它写了以下代码,但这些调用的响应大部分时间是随机的,即有时响应状态码为200,有时响应状态码为304(预期)。我是不是在代码中做错了什么,或者我应该记住AFNetworking有什么特别的地方。
NSURL *url = [NSURL URLWithString:@"http://ia.media-imdb.com/images/M/MV5BNzI0NTY5NzQwMV5BMl5BanBnXkFtZTcwOTQyNTA5OA@@._V1._SY90_.jpg"];
NSMutableURLRequest *aRequest = [NSMutableURLRequest requestWithURL:url];
[aRequest setValue:@"\"61-smtLpBSL_SY90_#1\"" forHTTPHeaderField:@"If-None-Match"];
NSLog(@"headers: %@", aRequest.allHTTPHeaderFields);
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:aRequest imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"%@ %d", response.allHeaderFields, response.statusCode);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Request failed with error: %@", error);
}];
[operation start];发布于 2013-08-07 16:23:55
我也遇到了同样的问题,我设法通过将我的请求更改为
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:60];发布于 2013-01-03 16:34:29
我已经尝试了你的代码,我收到了状态200 - OK,这是它应该是。
代码304 -未修改,我无法获取。我已经试过几次快速和缓慢地运行命令,但我总是得到200作为响应。
当你得到304作为响应码的时候,你有更多的细节吗?
304响应码通常意味着内容没有被修改,你应该得到包含200作为响应的原始请求。
在这里您可以阅读更多关于Etag:ETag vs Header Expires的内容
发布于 2015-06-03 23:44:45
我在Etag逻辑和AFNetworking方面也遇到了问题。我的问题只是cachePolicy设置不正确...
var manager = AFHTTPRequestOperationManager()
manager.requestSerializer = AFHTTPRequestSerializer()
manager.requestSerializer.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
manager.requestSerializer.setValue(etag, forHTTPHeaderField: "If-None-Match")
manager.GET(fullPath, parameters: parameters, success: {}, failure: {}))我希望它能帮助一些人!
https://stackoverflow.com/questions/14134226
复制相似问题