如何获取响应头信息
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取响应头信息相关的知识,希望对你有一定的参考价值。
参考技术A 最近七牛云服务器频繁报 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";
以上是关于如何获取响应头信息的主要内容,如果未能解决你的问题,请参考以下文章