1

我想知道哪个方法是验证部分,它将在响应中识别缓存控制标头并将调用connection:willCacheResponse:委托。

因为在这里我已经自定义了响应标头并希望NSURLConnection调用connection:willCacheResponse:,但这并没有发生。

- (void)connection:(NSURLConnection __unused *)connection

didReceiveResponse:(NSURLResponse *)response {

NSHTTPURLResponse *httpresponse = (NSHTTPURLResponse *)response;
NSLog(@"headers : %@", [httpresponse allHeaderFields]);
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
[mutableDictionary addEntriesFromDictionary:[httpresponse allHeaderFields]];
[mutableDictionary setObject:@"public, must-revalidate, max-age=300000" forKey:@"Cache-Control"];
NSURLRequest *urlRequest = [connection currentRequest];

NSInteger statusCode = 200;
//id headerFields = nil;
NSURL *urlString = urlRequest.URL;
NSString* HTTPVersion = nil;
SEL selector = NSSelectorFromString(@"initWithURL:statusCode:HTTPVersion:headerFields:");
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
    NSInvocation *inv = [NSInvocation invocationWithMethodSignature:signature];
[inv setTarget:httpresponse];
[inv setSelector:selector];
[inv setArgument:&urlString atIndex:2];
[inv setArgument:&statusCode atIndex:3];
[inv setArgument:&mutableDictionary atIndex:5];
[inv setArgument:&HTTPVersion atIndex:4];
[inv invoke];    

NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)self.response;
int code = [httpResponse statusCode];
NSLog(@"response status code : %d", code);
if ([self.response respondsToSelector:@selector(allHeaderFields)]) {
    NSDictionary *dictionary = [httpResponse allHeaderFields];
    NSLog(@"Header Discription:%@", [dictionary description]);
}

[self.outputStream open];

}

4

0 回答 0