没有公开的方式来访问NSHTTPURLResponse
实例的 http 版本,响应的版本取决于请求的版本。
CFNetworking
如果你真的想访问 http 版本,你可以使用。
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);