使用NSURLSession设置cookie [英] Set cookies with NSURLSession

查看:639
本文介绍了使用NSURLSession设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在开发一个Iphone应用程序我想在服务器响应后设置cookie并将其用于另一个请求。我的网络请求看起来像。

Hi I am developing one Iphone application In which I want to set cookies after server response and use that for another request. My network request looks like.

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
    NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
    NSLog(@"sttaus code %i", httpResp.statusCode);
    if (error) {
        [self.delegate signinWithError:error];
    }
    else {
        [self.delegate signinWithJson:data];
    }
}] resume];

但我不知道如何设置cookie。我知道我必须使用 NSHTTPCookieStorage 但我不知道如何设置。而且我也希望将cookie用于其他请求。有没有人知道这件事?需要帮忙。谢谢。

But I don't know how to set cookies. I know I have to use NSHTTPCookieStorage but I don't know how to set. And I also want to use that cookies for another request. Is there any one who knows about this? Need help. Thank you.

看到我试过这种方式

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{      
    if(error) {
        [self.delegate signinWithError:error];
    }
    else {
        NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;

        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
        NSHTTPCookie *cookie;

        NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];

        NSDictionary* headers = [(NSHTTPURLResponse *)response allHeaderFields];

        for(NSString *key in [headers allKeys]) {
            NSLog(@"%@ ..PPPP ... %@",key ,[headers objectForKey:key]);
            [cookieProperties setObject:[headers objectForKey:key] forKey:key];
        }

        [cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];

        cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

        [self.delegate signinWithJson:data];
    }
}] resume];

我只对一个标题字段感兴趣 Set-Cookie SSID = kgu62c0fops35n6qbf12anqlo7; path = /

I am interested in only one header field Set-Cookie SSID=kgu62c0fops35n6qbf12anqlo7; path=/

推荐答案

你可能只需使用 <$ href =https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookieStorage_Class/ sharedHTTPCookieStorage 参考/ Reference.htmlrel =nofollow noreferrer> NSHTTPCookieStorage ,然后使用 setCookies:forURL:mainDocumentURL: 或单个 setCookie: - 后者可能更符合您的需求。

You can probably get away with just using the sharedHTTPCookieStorage for NSHTTPCookieStorage, and then use setCookies:forURL:mainDocumentURL: or the single setCookie: - the latter might be better for your needs.

如果这不起作用,您可能需要设置 NSURLSessionConfiguration 并设置 NSHTTPCookieStorage

If this doesn't work you might need to setup the NSURLSessionConfiguration and set the NSHTTPCookieStorage

docs没有说明,但 defaultSessionConfiguration 可能使用共享存储无论如何。

The docs don't state it, but the defaultSessionConfiguration might use the shared store anyway.

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
    NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;

    NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[response URL]];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:[response URL] mainDocumentURL:nil];

    NSLog(@"sttaus code %i", httpResp.statusCode);
    if (error) {
        [self.delegate signinWithError:error];
    }
    else {
        [self.delegate signinWithJson:data];
    }
}] resume];

这篇关于使用NSURLSession设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆