如何获取响应头信息

最近七牛云服务器频繁报 HTTP 416 请求范围无法满足错误,出现改错误的原因是request请求Range设置的范围超出了资源的范围。例如,如果一个图像文件资源有1000个字节,而被请求的范围是500-1500,那就无法满足。

那么设置Range的时候如何获取到服务器上的资源信息呢? 毫无疑问我们需要分析请求头信息。

-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response

我们打印response观察一下

(lldb) po response

{ URL: http://f1.ichazuo.cn/1507718418684962.mp3 } { status code: 404, headers {

"Access-Control-Allow-Origin" = "*";

"Access-Control-Expose-Headers" = "X-Log, X-Reqid";

"Access-Control-Max-Age" = 2592000;

Connection = "keep-alive";

"Content-Length" = 30;

"Content-Type" = "application/json";

Date = "Wed, 11 Oct 2017 11:33:00 GMT";

EagleId = 3c1ce20115077215791328289e;

Server = Tengine;

"Timing-Allow-Origin" = "*";

Via = "cache8.l2nu16-1[387,404-1280,M], cache10.l2nu16-1[1015,0], kunlun5.cn36[1032,404-0,M], kunlun1.cn36[1045,0]";

"X-Cache" = "MISS TCP_MISS dirn:-2:-2 mlen:-1";

"X-Log" = "mc.g/404;rs4_3.sel:1/not found;rdb.g/no such key;DBD/404;v4.get/Document not found;rwro.get:2/Document not found;RS.dbs:2/Document not found;RS:2/404;mc.g/404;rs4_3.sel:1/not found;rdb.g/no such key;DBD/404;v4.get/Document not found;rwro.get:1/Document not found;RS.dbs:1/Document not found;RS:2/404;IO:6/404";

"X-M-Log" = "QNM:xs444;SRCPROXY:xs489;SRCPROXY:37/404;QNM3:311/404";

"X-M-Reqid" = I3kAAHdA7eLJgOwU;

"X-Qnm-Cache" = "Validate,Proxy,CacheA";

"X-Reqid" = I3kAAHdA7eLJgOwU;

"X-Svr" = IO;

"X-Swift-CacheTime" = 1;

"X-Swift-Error" = "orig response 4XX error";

"X-Swift-SaveTime" = "Wed, 11 Oct 2017 11:33:00 GMT";

} }

从打印的信息可以看到相应头的信息,但是我们如何在程序中获取到这些信息呢,我们打印一下response看一下

(lldb) po [response class]

NSHTTPURLResponse

由打印信息可知response是NSHTTPURLResponse类,而不是NSURLResponse类,之所以能够用NSURLResponse变量接受想必大家也猜到了NSURLResponse是NSHTTPURLResponse类的父类,用父类的指针接收字累的对象,也就是多态。

具体关于NSHTTPURLResponse类的具体情况如下:

/*!

@class NSHTTPURLResponse

@abstract An NSHTTPURLResponse object represents a response to an

HTTP URL load. It is a specialization of NSURLResponse which

provides conveniences for accessing information specific to HTTP

protocol responses.

*/

@interfaceNSHTTPURLResponse :NSURLResponse

{

@package

NSHTTPURLResponseInternal*_httpInternal;

}

/*!

@methodinitWithURL:statusCode:HTTPVersion:headerFields:

@abstract initializer for NSHTTPURLResponse objects.

@paramurl the URL from which the response was generated.

@paramstatusCode an HTTP status code.

@paramHTTPVersion The version of the HTTP response as represented by the server.This is typically represented as "HTTP/1.1".

@paramheaderFields A dictionary representing the header keys and values of the server response.

@resultthe instance of the object, or NULL if an error occurred during initialization.

@discussion This API was introduced in Mac OS X 10.7.2 and iOS 5.0 and is not available prior to those releases.

*/

- (nullableinstancetype)initWithURL:(NSURL*)url statusCode:(NSInteger)statusCode HTTPVersion:(nullableNSString*)HTTPVersion headerFields:(nullableNSDictionary *)headerFieldsAPI_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));

/*!

@abstract Returns the HTTP status code of the receiver.

@result The HTTP status code of the receiver.

*/

@property(readonly)NSIntegerstatusCode;

/*!

@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;

/*!

@method localizedStringForStatusCode:

@abstract Convenience method which returns a localized string

corresponding to the status code for this response.

@param statusCode the status code to use to produce a localized string.

@result A localized string corresponding to the given status code.

*/

+ (NSString*)localizedStringForStatusCode:(NSInteger)statusCode;

@end

到现在为止要获取相应头的信息就很明确了:只要把response强转成NSHTTPURLResponse然后用allHeaderFields属性获取即可。通过allHeaderFields获取到的是相应头信息的字典,通过相应的key就可以获取到value了。

NSLog(@"%@",((NSHTTPURLResponse*)response).allHeaderFields);

2017-10-11 19:44:41.191104+0800 chazuoxy[3249:915274] {

"Accept-Ranges" = bytes;

"Access-Control-Allow-Origin" = "*";

"Access-Control-Expose-Headers" = "X-Log, X-Reqid";

"Access-Control-Max-Age" = 2592000;

"Cache-Control" = "public, max-age=31536000";

Connection = "keep-alive";

"Content-Disposition" = "inline; filename=\"1495179795459228.mp3\"; filename*=utf-8' '1495179795459228.mp3";

"Content-Length" = 1048577;

"Content-Range" = "bytes 0-1048576/9564263";

"Content-Transfer-Encoding" = binary;

"Content-Type" = "audio/mpeg";

Date = "Wed, 11 Oct 2017 11:44:29 GMT";

EagleId = 791d088515077222695406541e;

Etag = "\"liSdTGgMGz83QVxPnwjdViPWw3xY\"";

"Last-Modified" = "Wed, 30 Aug 2017 07:03:11 GMT";

Server = Tengine;

"Timing-Allow-Origin" = "*";

Via = "cache23.l2nu16-1[324,200-0,M], cache40.l2nu16-1[359,0], kunlun5.cn410[414,206-0,M], kunlun5.cn410[416,0]";

"X-Cache" = "MISS TCP_MISS dirn:-2:-2 mlen:-1";

"X-Log" = "mc.g/404;rs4_1.sel:1;rwro.get:1;RS.dbs:1;RS:1;mc.s;PFDT:1;DC:15/404;fs0EBD;mc.g/404;EBDMASTER;mc.s;m.Get:1;EBDDN:1;IO:51";

"X-M-Log" = "QNM:xs467;SRCPROXY:xs484;SRCPROXY:88;QNM3:164";

"X-M-Reqid" = jyMAAIQRDX5qgewU;

"X-Qiniu-Zone" = 1;

"X-Qnm-Cache" = "Miss,Proxy,CacheA";

"X-Reqid" = jyMAAIQRDX5qgewU;

"X-Svr" = IO;

"X-Swift-CacheTime" = 2592000;

"X-Swift-SaveTime" = "Wed, 11 Oct 2017 11:44:29 GMT";

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,672评论 18 139
  • 我们或许可以把欧元当作一种最小范围内的“全球”统一的货币,因为欧元作为一部分欧洲国家共同在使用的货币。我们...
    小小杠杆阅读 1,182评论 1 2
  • 有怎样的年龄,就有怎样的人生使命;有怎样的年龄,就有怎样的处世心境;有怎样的年龄,就有怎样的办事思维和方法。一个人...
    彤祥阁阅读 388评论 0 0
  • 从五月初到现在,有多次五公里跑步,每次状态又不一样!有时还能刷新一下PB!但要是放在十公里的框架上,PB就不应该当...
    Cinko阅读 539评论 0 1
  • 九方皋相马雌雄黑白不是关键,关键你是不是千里马。最可怕的品种是如我一样是:千里驴。 秋日里剥离倦虫的低鸣...
    深蓝色木鱼阅读 157评论 0 0