我正在使用新版本的AFNetworking,我无法弄清楚如何阅读响应的标题.我正在使用AFHTTPSessionManager来执行我的查询,一切正常但我无法找到标题响应字段.
这是我如何进行
self.sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]]; [self.sessionManager GET:urlId parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) { if ([self.delegate respondsToSelector:@selector(userIsLoadedWithInfos:)]) { [self.delegate userIsLoadedWithInfos: responseObject]; } } failure:^(NSURLSessionDataTask *task, NSError *error) { if ([self.delegate respondsToSelector:@selector(userLoadingFailed)]) { [self.delegate userLoadingFailed]; } } ];
我尝试读取任务的响应属性,但它返回一个不包含标题的NSURLResponse.现在有人如何阅读2.0版本的响应标头?谢谢
你有没有试过从NSURLResponse获取返回的头文件,
您可以尝试使用NSURLResponse对象,
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; if ([httpResponse respondsToSelector:@selector(allHeaderFields)]) { NSDictionary *dictionary = [httpResponse allHeaderFields]; NSLog([dictionary description]); }
希望对你有帮助.!
一个比Viruss mcs更强大的代码:
if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) { NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response; NSLog(@"%@" ,[r allHeaderFields]); }
回报
{ Connection = "Keep-Alive"; "Content-Length" = 12771; "Content-Type" = "application/json"; Date = "Fri, 06 Dec 2013 10:40:48 GMT"; "Keep-Alive" = "timeout=5"; "Proxy-Connection" = "Keep-Alive"; Server = "gunicorn/18.0"; }
同样地,你可以确保施放是正确的[response respondsToSelector:@selector(allHeaderFields)]
,但你也应该在进行施法之前调用它
if ([task.response respondsToSelector:@selector(allHeaderFields)]) { NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response; NSLog(@"%@" ,[r allHeaderFields]); }
或根本没有演员表:
if ([task.response respondsToSelector:@selector(allHeaderFields)]) { NSLog(@"%@" ,[task.response performSelector:@selector(allHeaderFields)]); }