根据HTTP的协议规定,304表示缓存,200表示成功,其他表示失败。AFNetworking默认是把304的Code变成200了,然后去拿缓存数据。这个在我们需要自定义304策略的时候会有问题。解决方案是为AFHTTPSessionManager的requestSerializer设置标准行为.

 [httpSessionManager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];


附一份自己的代码,免得下次进来忘记了。

+(NSURLSessionDataTask *)productGET:(NSString *)interfacePath
                          tableType:(NSString *)page
                            success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                            failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    [EasyNetwork userIdSuccess:^(NSString *userId) {
        AFHTTPSessionManager *httpSessionManager =[AFHTTPSessionManager manager];
        httpSessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
        [httpSessionManager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
        NSString *key = [NSString stringWithFormat:@"%@-%@", @"If-Modified-Since", page];
        NSString *date = [[LocalCache sharedCache] cachedObjectForKey:key];
        
        if(date != nil && ![date isEqualToString:@""]){
            [httpSessionManager.requestSerializer setValue:@"Wed, 23 Oct 2014 05:13:30 GMT"  //date
                                        forHTTPHeaderField:@"If-Modified-Since"];
        }
        
        NSString *path = [NSString stringWithFormat:@"%@%@&userId=%@", kBaseURL, interfacePath, userId];
        path =  [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [httpSessionManager GET:path
                     parameters:nil
                        success:^(NSURLSessionDataTask *task, id responseObject){
                            NSString *tmp = [httpSessionManager.requestSerializer valueForHTTPHeaderField:@"If-Modified-Since"];
                            NSLog(@"Request  If-Modified-Since:%@", tmp);
                            if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
                                NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response;
                                NSLog(@"StatusCode:%d" ,[r statusCode]);
                                NSDictionary *header = [r allHeaderFields];
                                NSString *date = [header objectForKey:@"Last-Modified"];
                                [[LocalCache sharedCache] storeCacheObject:date
                                                                    forKey:key];
                            }
                            success(task, responseObject);
                        }
                        failure:^(NSURLSessionDataTask *task, NSError *error) {
                            if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
                                NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response;
                                NSLog(@"StatusCode:%d" ,[r statusCode]);
                                NSDictionary *header = [r allHeaderFields];
                                NSString *date = [header objectForKey:@"Last-Modified"];
                                [[LocalCache sharedCache] storeCacheObject:date
                                                                    forKey:key];
                            }
                            
                            failure(task, error);
                        }
         ];
    } userIdFailure:^{
        failure(nil, nil);
    }];
    return nil;
}