码迷,mamicode.com
首页 > 移动开发 > 详细

iOS原生请求获取Cookie值

时间:2017-10-12 14:58:48      阅读:448      评论:0      收藏:0      [点我收藏+]

标签:ble   afn   das   sky   har   int   fork   send   timeout   

 1     //创建URL
 2     NSURL * url = [NSURL URLWithString:@"http://api.skyfox.org/api-test.php"];
 3     
 4     //创建request请求
 5     NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:3];
 6     
 7     //初始化请求队列
 8     NSOperationQueue *queue = [NSOperationQueue mainQueue];
 9     
10     [NSURLConnection sendAsynchronousRequest:request
11                                        queue:queue
12                            completionHandler:
13      ^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
14          
15          //转换NSURLResponse成为HTTPResponse
16          NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
17          
18          //获取headerfields
19          
20          //原生NSURLConnection写法
21          NSDictionary *fields = [httpResponse allHeaderFields];
22          
23          //afnetworking写法
24          //NSDictionary *fields = [operation.response allHeaderFields];
25          
26          
27          //获取cookie方法1
28          NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:fields forURL:url];
29       
30          for (NSHTTPCookie * cookie in cookies) {
31              
32              NSDictionary * cookieDic = [cookie properties];
33              NSLog(@"%@",cookieDic);
34          }
35          
36          //获取cookie方法2
37          /*
38           NSString *cookieString = [[HTTPResponse allHeaderFields] valueForKey:@"Set-Cookie"];
39           */
40          
41          
42          //获取cookie方法3
43          /*
44           NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
45           
46           for (NSHTTPCookie *cookie in [cookieJar cookies])
47           {
48           NSLog(@"cookie%@", cookie);
49           }
50           */
51     }];

 

iOS原生请求获取Cookie值

标签:ble   afn   das   sky   har   int   fork   send   timeout   

原文地址:http://www.cnblogs.com/absty-guo/p/7656039.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!