gpt4 book ai didi

objective-c - 从 NSHTTPURLResponse 获取多个 Set-Cookie header

转载 作者:行者123 更新时间:2023-12-03 16:40:22 30 4
gpt4 key购买 nike

我正在尝试访问在 http 响应中发送的 Set-Cookie header :

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSDictionary *headers = [httpResponse allHeaderFields];
NSLog([headers description]);
}

HTTP 服务器发送两个 Set-Cookie header ,如下所示:

Set-Cookie: foo=1; httponly; Path=/
Set-Cookie: bar=2; httponly; Path=/

但是,[HTTPResponse allHeaderFields]接口(interface)将多个 Set-Cookie header 组合成一个逗号分隔的字符串:

"Set-Cookie" = "foo=1; httponly; Path=/, bar=2; httponly; Path=/"

This cocoa-dev mailing list message证实了我的观察。

是否有一个接口(interface)可以单独获取每个 Set-Cookie header 或访问原始 header ?

最佳答案

iOS 和 macOS 定义了一个 NSHTTPCookie 类,它可以解析并为您提供所需的信息:

NSArray<NSHTTPCookie *> *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: [httpResponse allHeaderFields] forURL: [httpResponse url]];

有关详细信息,请参阅 documentation对于NSHTTPCookie

关于objective-c - 从 NSHTTPURLResponse 获取多个 Set-Cookie header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42323358/

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