AFNetworking 2.0 //如何读取响应头

 手机用户2502854133 发布于 2023-02-12 14:54

我正在使用新版本的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版本的响应标头?谢谢

2 个回答
  • 你有没有试过从NSURLResponse获取返回的头文件,

    您可以尝试使用NSURLResponse对象,

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
    if ([httpResponse respondsToSelector:@selector(allHeaderFields)]) {
        NSDictionary *dictionary = [httpResponse allHeaderFields];
        NSLog([dictionary description]);
    }
    

    希望对你有帮助.!

    2023-02-12 14:56 回答
  • 一个比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)]);
    }
    

    2023-02-12 14:56 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有