AFNetworking 设置cookie

AFNetworking 设置cookie_第1张图片
Snip20170712_13.png

图中cookie有三个键值对,其中第一个属于一个站点,第二个和第三个属于另一个站点。
为每一个键值对创建一个dictionary如下代码所示:
其中,NSHTTPCookieDomain对应图中主机地址,NSHTTPCookiePath对应图中路径,NSHTTPCookieName对应cookie名称,NSHTTPCookieValue对应cookie内容;

NSDictionary *properties1 = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"domain1.com", NSHTTPCookieDomain,
                             @"/cas/", NSHTTPCookiePath,
                             @"SESSION", NSHTTPCookieName,
                             @"771c4352-8a1b-4d86-9fc6-bf99dbd63790", NSHTTPCookieValue,
                             nil];

NSDictionary *properties2 = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"domain2.com", NSHTTPCookieDomain,
                             @"/cas", NSHTTPCookiePath,
                             @"CASTGC", NSHTTPCookieName,
                             @"TGT-111891-1ptgGfEBwfPmFZXWPkRHHvn0elCyBwnJ4XHcLrVsh0hM5H1ziA-lvzhou.h3c.com", NSHTTPCookieValue,
                             nil];

NSDictionary *properties3 = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"domain2.com", NSHTTPCookieDomain,
                             @"/", NSHTTPCookiePath,
                             @"connect.sid", NSHTTPCookieName,

@"s%3AKyUKJtrby_1lKr4YCIc0sTyjDG7TGiId.Ck%2BjsdVCzNL5XlR2RZkNybBzRs6LK7J2mzPldnRLKGU", NSHTTPCookieValue,
nil];

利用dictionary创建NSHTTPCookie对象实例:

NSHTTPCookie *cookie1 = [NSHTTPCookie cookieWithProperties:properties1];
NSHTTPCookie *cookie2 = [NSHTTPCookie cookieWithProperties:properties2];
NSHTTPCookie *cookie3 = [NSHTTPCookie cookieWithProperties:properties3];

将cookie添加到NSHTTPCookieStorage
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie1];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie2];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie3];

你可能感兴趣的:(AFNetworking 设置cookie)