iOS Http请求获取响应头文件 与添加头文件
2016-07-06 15:18
627 查看
一直都是在给服务器端发送请求的时候可能会出现设置头文件的情况,但这次获取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 fieldsof the receiver.@discussion By examining this header dictionary, clients can seethe "raw" header information which was reported to the protocolimplementation by the HTTP server. This may be of use tosophisticated or special-purpose HTTP clients.@result A dictionary containing all the HTTP header fields of thereceiver.*/@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;
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法