沒有公開的方式來訪問NSHTTPURLResponse
實例的http版本,並且響應版本取決於請求版本。如果你真的想訪問http版本,你可以使用CFNetworking
。
CFN_EXPORT CFHTTPMessageRef
CFHTTPMessageCreateResponse(
CFAllocatorRef __nullable alloc,
CFIndex statusCode,
CFStringRef __nullable statusDescription,
CFStringRef httpVersion) CF_AVAILABLE(10_1, 2_0);
而且CFHTTPMessageCopyVersion()
返回的HTTP版本。
其實-[NSHTTPURLResponse initWithURL:(NSURL *)URL statusCode:(NSInteger)statusCode HTTPVersion:(NSString *)version headerFields:(NSDictionary *)fields]
使用CFHTTPMessageCreateResponse
創建一個HTTP響應。請參閱NSURLResponse.m
NSURLResponse
支持_CFURLResponse
結構。
typedef struct _CFURLResponse {
CFRuntimeBase _base;
CFAbsoluteTime creationTime;
CFURLRef url;
CFStringRef mimeType;
int64_t expectedLength;
CFStringRef textEncoding;
CFIndex statusCode;
CFStringRef httpVersion;
CFDictionaryRef headerFields;
Boolean isHTTPResponse;
OSSpinLock parsedHeadersLock;
ParsedHeaders* parsedHeaders;
} _CFURLResponse;
typedef const struct _CFURLResponse* CFURLResponseRef;
你可以使用_CFURLResponse
getter方法這個結構上NSURLResponse
實例:
CFTypeRef test = CFBridgingRetain([response performSelector:NSSelectorFromString(@"_CFURLResponse")]);
CFShow(test);