从NSHTTPURLResponse
文档中,我找不到获取HTTP
版本(i.e. "HTTP/1.1" or "HTTP/1.0")
的方法。
HTTP
标头字段可通过所有标头字段属性获得,但标头字段中未提供HTTP
协议版本,因为它不是HTTP
标头。
注意: init,NSHTTPURLResponse
初始化程序
(initWithURL:statusCode:HTTPVersion:headerFields:)
确实需要HTTPVersion
作为输入,因此理论上它可能已保存在那里。
但是,我找不到从给定响应中获取HTTPVersion
的属性或方法,例如从NSURLSessionDataTask返回的响应。
答案 0 :(得分:3)
没有公开的方式来访问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
实例上使用NSURLResponse
getter方法获取此结构:
CFTypeRef test = CFBridgingRetain([response performSelector:NSSelectorFromString(@"_CFURLResponse")]);
CFShow(test);
答案 1 :(得分:0)
请不要使用_CFURLResponse
私有API,因为CFNetwork不能保证其返回值的格式。
查询HTTP版本的推荐方法是实现URLSession:task:didFinishCollectingMetrics:
委托方法