我正在使用AFNetworking 1.3.3從Amazon S3中獲取靜態JSON文件。如果我使用Google Chrome請求這些文件並檢查標題,它會在請求中發送一個If-Modified-Since
和If-None-Match
HTTP標頭,並且您會收到一個很好的304響應。AFNetworking請求中忽略If-Modified-Since或If-None-Match標頭
但是,當我使用AFNetworking發送相同的請求時,發送相同的If-Modified-Since
和相同的If-None-Match
標題,我每次都得到200,即使我知道內容未被修改。它似乎忽略了我發送的額外頭文件。
這裏是我的代碼:
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:feedUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
if (feed.remoteLastModified != nil) {
// In real life I use the last modified header returned by the request
[request setValue:@"Tue, 10 Dec 2013 07:15:50 GMT" forHTTPHeaderField:@"If-Modified-Since"];
}
if (feed.remoteETag.length > 0) {
NSLog(@"If-None-Match: %@", feed.remoteETag);
[request setValue:feed.remoteETag forHTTPHeaderField:@"If-None-Match"];
}
NSLog(@"headers: %@", request.allHTTPHeaderFields);
AFJSONRequestOperation* operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest* request, NSHTTPURLResponse* response, id JSON) {
NSLog(@"Status code: %d", response.statusCode);
} failure:^(NSURLRequest* request, NSHTTPURLResponse* response, NSError* error, id JSON) {
if (response.statusCode != 304) {
NSLog(@"There was an error: %@", [error userInfo]);
} else {
NSLog(@"Not modified");
}
}];
[operation start];