iOS 网络通信过程 cookie 的使用

一.Cookie的获取
如果获取的 Cookie 为空,那么发起一个网络请求即可产生 Cookie
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieJar cookies]) {
   NSLog(@"%@", cookie);
}
二. Cookie 的清除
获取所有 Cookie, 然后删除
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *_tmpArray = [NSArray arrayWithArray:[cookieJar cookies]];
for (id obj in _tmpArray) {
  [cookieJar deleteCookie:obj];
}
三. Cookie 的设置
设置指定的Cookie,这样做只会设置Cookie到系统,在进行网络请求的时候并不会带着Cookie到服务端.
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"username" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"password" forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"io" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"com" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
上面说了Cookie的一些操作,现在对如何在请求中带着cookie通信
1.苹果自带网络请求库携带cookie通信
//Cookie的基本设置
NSMutableDictionary *properties = [[NSMutableDictionary alloc] init];
[properties setValue:cookieValue forKey:NSHTTPCookieValue];
[properties setValue:cookieKey forKey:NSHTTPCookieName];
[properties setValue:@"" forKey:NSHTTPCookieDomain];
[properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
[properties setValue:@"/" forKey:NSHTTPCookiePath];

NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:properties];
NSArray *cookies=[NSArray arrayWithObjects:cookie,nil];
NSDictionary *headers=[NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

//将cookie塞进Request请求
NSURL *url = [NSURL URLWithString:pathStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.timeoutInterval = self.timeoutInterval;
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[headers objectForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"];
这样就可以把cookie带到服务端进行通信了
2.AFN携带cookie通信
NSString *cookieValue = @"1111111111";
NSString *cookieKey = @"iPlanetDirectoryPro";

//Cookie的基本设置
NSDictionary *properties = [[NSMutableDictionary alloc] init];
[properties setValue:cookieValue forKey:NSHTTPCookieValue];
[properties setValue:cookieKey forKey:NSHTTPCookieName];
[properties setValue:@"" forKey:NSHTTPCookieDomain];
[properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
[properties setValue:@"/" forKey:NSHTTPCookiePath];

NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:properties];
NSArray *cookies=[NSArray arrayWithObjects:cookie,nil];
NSDictionary *headers=[NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:tokenIdValue forKey:tokenIdKey];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//将Cookie绑定request请求
[manager.requestSerializer setValue:[headers objectForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"];

[manager GET:urlString parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];

上述2种方式亲测可行
本文参考 ios 网络通信过程cookie的使用,非常感谢该作者.

你可能感兴趣的:(iOS 网络通信过程 cookie 的使用)