一直都是在给服务器端发送请求的时候可能会出现设置头文件的情况,但这次获取HTTP 返回的头文件,着实让我纠结一番,但最终还是实现了,总结一下。(PS:其实最后方法很简单,只是分享一下纠结过程)

先看一下使用 AFNetworking3.0是如何获取数据的。

AFHTTPSessionManager *httpsManager = [AFHTTPSessionManager manager];
httpsManager.requestSerializer = [AFHTTPRequestSerializer serializer];
httpsManager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFSecurityPolicy *security = [AFSecurityPolicy defaultPolicy];
security.allowInvalidCertificates = YES;
security.validatesDomainName = NO;
httpsManager.securityPolicy = security;
[httpsManager POST:ZCFormatURL(@"/paydone") parameters:params progress:^(NSProgress * _Nonnull uploadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];
添加头文件:

[httpsManager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"os_type"];

[httpsManager.requestSerializer setValue:[info getNowTime] forHTTPHeaderField:@"time"];


分析一下返回的数据
failure 时:

  • NSURLSessionDataTask * _Nullable task 和请求有关的一些描述
  • NSError * _Nonnull error 网络请求不通等错误描述
    success 时:
  • NSURLSessionDataTask * _Nonnull task 和请求有关的一些描述
  • id _Nullable responseObject AFNetworking格式化之后的结果,与AFHTTPSessionManager的responseSerializer相对应

很明显,我们所要的数据最有可能是在 task 中,所以那就看一下NSURLSessionDataTask类吧,

/*
* An NSURLSessionDataTask does not provide any additional
* functionality over an NSURLSessionTask and its presence is merely
* to provide lexical differentiation from download and upload tasks.
*/
@interface NSURLSessionDataTask : NSURLSessionTask
@end

发现它仅仅只是继承自NSURLSessionTask,并没有自己的属性方法,好,那就接着看父类NSURLSessionTask
父类属性倒是不少(太长了,代码不放这了)。最有可能包含我们所要信息应该就是response属性了

@property (nullable, readonly, copy) NSURLResponse *response;         /* may be nil if no response has been received */

好,那就接着看NSURLResponse,发现他的属性方法中没有能获取头文件的。倒是他的子类中有一个属性挺顺眼的。

/*!
@method allHeaderFields
@abstract Returns a dictionary containing all the HTTP header fields
of the receiver.
@discussion By examining this header dictionary, clients can see
the "raw" header information which was reported to the protocol
implementation by the HTTP server. This may be of use to
sophisticated or special-purpose HTTP clients.
@result A dictionary containing all the HTTP header fields of the
receiver.
*/
@property (readonly, copy) NSDictionary *allHeaderFields;

赶紧回去判定一下返回的task.response是不是NSURLResponse的子类

if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
NSLog(@"The return class is subclass %@",NSStringFromClass([NSHTTPURLResponse class]));
}else{
NSLog(@"The return class is not subclass %@",NSStringFromClass([NSHTTPURLResponse class]));
}

打印日志:
2016-01-15 11:29:52.547 demo[535:106586] The return class is subclass NSHTTPURLResponse

果真是,这下就好办多了,直接强转,获取数据就好了

NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
NSDictionary *allHeaders = response.allHeaderFields;

获取http请求响应头的更多相关文章

  1. js获取http请求响应头信息

    var req = new XMLHttpRequest(); req.open('GET', document.location, false); req.send(null); var heade ...

  2. VS2008 C++ 利用WinHttp API获取Http请求/响应头部Header

    http://www.cnblogs.com/LCCRNblog/p/3833472.html 这一篇博客中,实现了获取http请求/响应后的html源码,现在需要获取http请求/响应的头部Head ...

  3. wget/curl查看请求响应头信息

    wget / curl 是两个比较方便的测试http功能的命令行工具,大多数情况下,测试http功能主要是查看请求响应 头信息 ,而给这两个工具加上适当的命令行参数即可轻易做到,其实查man手册就能找 ...

  4. HTTP请求响应头信息

    HTTP请求响应头信息 请求:(request) 组成部分: 请求行 请求头 请求体 请求行:请求信息的第一行 格式:请求方式 访问的资源 协议/版本 例如:GET /day0801/1.html H ...

  5. LoadRunner 获取接口请求响应信息

    Action() { int nHttpRetCode; // 默认最大长度为256,get请求需注意缓存问题,需要根据content-length进行修改 web_set_max_html_para ...

  6. [转] LoadRunner 获取接口请求响应信息

    Action() { int nHttpRetCode; // 默认最大长度为256,get请求需注意缓存问题,需要根据content-length进行修改 web_set_max_html_para ...

  7. http状态码 以及请求响应头相关

    1xx消息[编辑] 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束.由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非 ...

  8. Django(十一)请求生命周期之响应内容(请求/响应 头/体)

    https://www.cnblogs.com/renpingsheng/p/7534897.html Django请求生命周期之响应内容 http提交数据的方式有"post",& ...

  9. C#爬虫之通过Selenium获取浏览器请求响应结果

    前言 在进行某些爬虫任务的时候,我们经常会遇到仅用Http协议难以攻破的情况,比如协议中带有加密参数,破解需要花费大量时间,那这时候就会用Selenium去模拟浏览器进行页面上的元素抓取 大多数情况下 ...

随机推荐

  1. Ubuntu使用ApkTool进行APK反编译

    1.Apktool下载 http://ibotpeaches.github.io/Apktool/ 下载最新版本Apktool_2.1.1.jar 2.新建一个apktool目录,将Apktool_2 ...

  2. leetcode 141. Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  3. 快速排序python实现

    #--×--coding:utf-8-*- def main(): nlist = [] while 1: tmp = raw_input("Please input your elemen ...

  4. homework160809207刘兆轩

    # include <stdio.h> int main() { float a,b,c,m,n,l,k,j,i; printf("请输入三个数:\n"); scanf ...

  5. hiho #1318 非法二进制数

    #1318 : 非法二进制数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 如果一个二进制数包含连续的两个1,我们就称这个二进制数是非法的. 小Hi想知道在所有 n 位 ...

  6. net-snmp配置文件详解

    net-snmp配置文件详解 net-snmp的配置文件是有一定的层次结构的,配置起来也很方便.网上找了很多资料,大概把这个配置文件的各个信息搞懂了一点.其实在net-snmp的EXAMPLE.con ...

  7. WebClient 访问https

    解决SSH证书问题: webClient.getOptions().setUseInsecureSSL(true);//解决ssh证书访问https的问题

  8. 基于Selenium的模拟浏览器采集

    Selenium 也是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7.8.9).Mozilla Firefox.Mozil ...

  9. @ModelAttribute运用详解

      @ModelAttribute使用详解 1.@ModelAttribute注释方法     例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个 ...

  10. Python分割list

    对于一个很大的列表,例如有超过一万个元素的列表,假如需要对列表中的每一个元素都进行一个复杂且耗时的计算,用单线程处理起来会很慢,这时有必要利用多线程进行处理,处理之前首先需要对大的列表进行分割,分割成 ...