10

我有一个非常奇怪的问题,我正在请求一个 URL,我想从中获取 cookie,我使用这种方式来获取 cookie:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSDictionary *fields = [HTTPResponse allHeaderFields];
    NSString *cookie = [fields valueForKey:"Set-Cookie"];
}

但是 cookie 不完整,缺少一些字段,我在 PostMan 上检查过,所有的 cookie 都在那里。

我在启动NSURLRequest.

[request setHTTPShouldHandleCookies:YES];

问题出在哪里?

注意:这个问题出现在 iOS 上,我有一个 android 版本,它工作正常,所有的 cookie 都在那里。

4

5 回答 5

15

您是否尝试过以下代码示例,它应该可以工作:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSArray *cookies =[[NSArray alloc]init]; 
    cookies = [NSHTTPCookie 
        cookiesWithResponseHeaderFields:[response allHeaderFields] 
        forURL:[NSURL URLWithString:@""]]; // send to URL, return NSArray
}
于 2013-09-28T11:57:25.190 回答
13

@biloshkurskyi.ss 答案是正确的。

我花了半天的时间试图找出为什么我的一些 cookie 没有出现在 iOS 上的 response.allHeaderFields 中,但它在 Android 上存在(使用相同的服务)。

原因是一些cookie被提前提取并存储在共享cookie存储中。它们不会出现在 allHeaderFields 中。

如果有人需要,这是答案的 swift 3 版本:

let request = URLRequest(url: myWebServiceUrl)
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: {
    (data, response, error) in
    if let error = error {
        print("ERROR: \(error)")
    } else {
        print("RESPONSE: \(response)")
        if let data = data, let dataString = String(data: data, encoding: .utf8) {
            print("DATA: " + dataString)
        }
        for cookie in HTTPCookieStorage.shared.cookies! {
            print("EXTRACTED COOKIE: \(cookie)") //find your cookie here instead of httpUrlResponse.allHeaderFields
        }
    }
})
task.resume()
于 2016-10-24T22:02:02.333 回答
10

试试这个代码:

for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
    NSLog(@"name: '%@'\n",   [cookie name]);
    NSLog(@"value: '%@'\n",  [cookie value]);
    NSLog(@"domain: '%@'\n", [cookie domain]);
    NSLog(@"path: '%@'\n",   [cookie path]);
}
于 2013-11-27T21:29:42.383 回答
2
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
    NSLog(@"name: '%@'\n",   [cookie name]);
    NSLog(@"value: '%@'\n",  [cookie value]);
    NSLog(@"domain: '%@'\n", [cookie domain]);
    NSLog(@"path: '%@'\n",   [cookie path]);
}

第二个代码是:

NSHTTPURLResponse *HTTPResponsesss = (NSHTTPURLResponse *)response;
NSDictionary *fields = [HTTPResponsesss allHeaderFields];
NSString *cookie = [fields valueForKey:@"Set-Cookie"]; // It is your cookie

NSLog(@"%@", cookie);

两个代码都可以正常工作。

于 2018-03-20T10:29:09.113 回答
0

它会起作用的。

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        NSString *cookieValue = @"";
        NSString *cookieHeaderValue = @"";
        NSString *finalCookieValue = @"";
        
        for (NSHTTPCookie *each in cookieStorage.cookies) {
            if ([each.name isEqualToString:@"ASP.NET_SessionId"]) {
                cookieHeaderValue = [cookieHeaderValue stringByAppendingString:each.name];
                cookieHeaderValue = [cookieHeaderValue stringByAppendingString:@"="];
                cookieHeaderValue = [cookieHeaderValue stringByAppendingString:each.value];
                cookieHeaderValue = [cookieHeaderValue stringByAppendingString:@";"];
            } else {
                cookieValue = [cookieValue stringByAppendingString:each.name];
                cookieValue = [cookieValue stringByAppendingString:@"="];
                cookieValue = [cookieValue stringByAppendingString:each.value];
                cookieValue = [cookieValue stringByAppendingString:@";"];
            }
        }
        
        finalCookieValue = [NSString stringWithFormat:@"%@%@", cookieHeaderValue, cookieValue];
于 2020-10-30T08:23:57.900 回答