最近七牛云服務(wù)器頻繁報(bào) HTTP 416 請(qǐng)求范圍無(wú)法滿足錯(cuò)誤唤冈,出現(xiàn)改錯(cuò)誤的原因是request請(qǐng)求Range設(shè)置的范圍超出了資源的范圍峡迷。例如,如果一個(gè)圖像文件資源有1000個(gè)字節(jié)你虹,而被請(qǐng)求的范圍是500-1500绘搞,那就無(wú)法滿足。
那么設(shè)置Range的時(shí)候如何獲取到服務(wù)器上的資源信息呢售葡? 毫無(wú)疑問(wèn)我們需要分析請(qǐng)求頭信息看杭。
-(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";
} }
從打印的信息可以看到相應(yīng)頭的信息忠藤,但是我們?nèi)绾卧诔绦蛑蝎@取到這些信息呢挟伙,我們打印一下response看一下
(lldb) po [response class]
NSHTTPURLResponse
由打印信息可知response是NSHTTPURLResponse類(lèi),而不是NSURLResponse類(lèi)模孩,之所以能夠用NSURLResponse變量接受想必大家也猜到了NSURLResponse是NSHTTPURLResponse類(lèi)的父類(lèi)尖阔,用父類(lèi)的指針接收字累的對(duì)象,也就是多態(tài)榨咐。
具體關(guān)于NSHTTPURLResponse類(lèi)的具體情況如下:
/*!
@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
到現(xiàn)在為止要獲取相應(yīng)頭的信息就很明確了:只要把response強(qiáng)轉(zhuǎn)成NSHTTPURLResponse然后用allHeaderFields屬性獲取即可介却。通過(guò)allHeaderFields獲取到的是相應(yīng)頭信息的字典,通過(guò)相應(yīng)的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";
}