gpt4 book ai didi

ios - 如何从给定的 NSHTTPURLResponse 获取 HTTP 协议(protocol)版本?

转载 作者:可可西里 更新时间:2023-11-01 17:00:31 51 4
gpt4 key购买 nike

NSHTTPURLResponse 文档中,我没有找到获取 HTTP 版本 (即“HTTP/1.1”或“HTTP/1.0”)的方法

HTTP header 字段可通过所有 Header Fields 属性使用,但是 HTTP 协议(protocol)版本未在 header 字段内给出,因为它不是 HTTP 标题。

注意: init,NSHTTPURLResponse初始化器(initWithURL:statusCode:HTTPVersion:headerFields:) 确实需要 HTTPVersion 作为输入,所以理论上它可能已经保存在那里。

但是,我找不到从给定响应中获取 HTTPVersion 的属性或方法,例如从 NSURLSessionDataTask 返回的响应。

最佳答案

没有公开的方法来访问 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;

您可以在 NSURLResponse 实例上使用 _CFURLResponse getter 方法获取此结构:

CFTypeRef test = CFBridgingRetain([response performSelector:NSSelectorFromString(@"_CFURLResponse")]);
CFShow(test);

关于ios - 如何从给定的 NSHTTPURLResponse 获取 HTTP 协议(protocol)版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39343725/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com