获取http请求响应头
一直都是在给服务器端发送请求的时候可能会出现设置头文件的情况,但这次获取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请求响应头的更多相关文章
- js获取http请求响应头信息
var req = new XMLHttpRequest(); req.open('GET', document.location, false); req.send(null); var heade ...
- VS2008 C++ 利用WinHttp API获取Http请求/响应头部Header
http://www.cnblogs.com/LCCRNblog/p/3833472.html 这一篇博客中,实现了获取http请求/响应后的html源码,现在需要获取http请求/响应的头部Head ...
- wget/curl查看请求响应头信息
wget / curl 是两个比较方便的测试http功能的命令行工具,大多数情况下,测试http功能主要是查看请求响应 头信息 ,而给这两个工具加上适当的命令行参数即可轻易做到,其实查man手册就能找 ...
- HTTP请求响应头信息
HTTP请求响应头信息 请求:(request) 组成部分: 请求行 请求头 请求体 请求行:请求信息的第一行 格式:请求方式 访问的资源 协议/版本 例如:GET /day0801/1.html H ...
- LoadRunner 获取接口请求响应信息
Action() { int nHttpRetCode; // 默认最大长度为256,get请求需注意缓存问题,需要根据content-length进行修改 web_set_max_html_para ...
- [转] LoadRunner 获取接口请求响应信息
Action() { int nHttpRetCode; // 默认最大长度为256,get请求需注意缓存问题,需要根据content-length进行修改 web_set_max_html_para ...
- http状态码 以及请求响应头相关
1xx消息[编辑] 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束.由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非 ...
- Django(十一)请求生命周期之响应内容(请求/响应 头/体)
https://www.cnblogs.com/renpingsheng/p/7534897.html Django请求生命周期之响应内容 http提交数据的方式有"post",& ...
- C#爬虫之通过Selenium获取浏览器请求响应结果
前言 在进行某些爬虫任务的时候,我们经常会遇到仅用Http协议难以攻破的情况,比如协议中带有加密参数,破解需要花费大量时间,那这时候就会用Selenium去模拟浏览器进行页面上的元素抓取 大多数情况下 ...
随机推荐
- 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型“System.ServiceModel.Activation.HttpModule”。
********************************* 思路壹(也是网络上铺天盖地的通俗解决方案) 原因: 这是因为先安装了 .NET Framework , 随后启用了 .NET Fra ...
- CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)
C. Mike and Frog time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 跨Controllers传数据
今天遇到两个问题,第一个是跨controller传值,后一个是比较简单的linq数据库查询问题.先描述以下问题我有一个入库单和一个入库明细,入库的逻辑是先填写入库单在填入库明细.两者要么同时完成,要么 ...
- MFC Edit控件 追加文本
// 追加文本到EditControl void InstmDebugMainDlg::AppendText(int controlId, CString strAdd) { ((CEdit* ...
- mysql python image
连接mysql数据库: cnx = mysql.connector.connect(user='joe', database='test') Connector/Python参数列表 Argument ...
- FineUI第十五天---表格概述
Grid表格概述 跟Asp.Net的差不多. 下面介绍一些属性: ExpandUnusedSpace:此列充满所有的剩余空间,并且随着表格宽度变化而变化 DataToolTipField:用来显示 ...
- Redis学习笔记十:独立功能之监视器
通过执行 monitor 命令可以让客户端自己变成一个监视器,实时接收并打印当前处理的命令请求的相关信息. 127.0.0.1:6379> monitor OK 1451752646.83727 ...
- /etc/bashrc和/etc/profile傻傻分不清楚?
导读 在一般的 linux 或者 unix 系统中, 都可以通过编辑 bashrc 和 profile来设置用户的工作环境, 很多文章对于 profile 和 bashrc 也都有使用, 但究竟每个文 ...
- leetcode 124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- IOI2015 Boxes
Description 给出一个环形,n个点,每次只能访问k个点,求最短距离. Sol 贪心. CCF的题解. 首先只会最多走一趟环形,根据抽屉原理,如果一边不足k个才会到另一边,所以对于第二次以上的 ...