CC's blog

CC的技术博客 专注于 it 互联网的技术爱好者

在Objective-c 中获取http 请求返回的Cookie 并存储 以及 设置请求的Cookie 方法

读取并存储

//获取cookie
 NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage]cookiesForURL:[NSURL URLWithString:url]];
  for (NSHTTPCookie *tempCookie in cookies)
{
    //打印cookies
    NSLog(@"getCookie:%@",tempCookie);
}
 NSDictionary *Request = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
 NSUserDefaults *userCookies = [NSUserDefaults standardUserDefaults];
 [userCookies setObject:[Request objectForKey:@"Cookie"] forKey:@"mUserDefaultsCookie"];
 [userCookies synchronize];

设置请求的 Cookie

[session.requestSerializer setValue:[[NSUserDefaults standardUserDefaults]objectForKey:@"mUserDefaultsCookie"]forHTTPHeaderField:@"Cookie"];
none

添加新评论