大醉和尚  
qq:3262641915,如有问题,欢迎讨论.

一直以为ios的http请求这块很简单应该不支持记录,保存,或者使用cookie,可是想当然归想当然,真用的时候,真研究了一下发现还真强大。经过一番的研究简单说一下我的理解:当你访问一个网站时,不管你愿意或者不愿意,NSURLRequest都会帮你主动记录下来你访问的站点设置的cookie,而且很负责任的,当你下次再访问这个站点时,NSURLRequest会拿着上次保存下来了的cookie继续去请求。这规律同样适用于ASIHTTPRequest。所以当你做一些基于认证的网络请求时,cookie不失为一个完美的解决方案。

那么怎么查看cookie呢?很简单:

NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieJar cookies]) {
   NSLog(@"%@", cookie);
}

这样就列出了所有已保存的cookie,如果当前为空怎么办呢?随便请求一个url喽。

 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://blog.cnrainbird.com"]
                                           cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
                                       timeoutInterval:3];
  
  [NSURLConnection sendSynchronousRequest:request 
                        returningResponse:nil
                                    error:nil];
  
  NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  for (NSHTTPCookie *cookie in [cookieJar cookies]) {
    NSLog(@"%@", cookie);
  }

  


是不是得到了类似:

1
<NSHTTPCookie version:0 name:"PHPSESSID" value:"evf5rcboo8th1dnl53fs4ukmt2" expiresDate:(null) created:2012-03-13 14:28:13 +0000 (3.53342e+08) sessionOnly:TRUE domain:"blog.cnrainbird.com" path:"/" isSecure:FALSE>

的东东?这就是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:@"rainbird" forKey:NSHTTPCookieValue];
  [cookieProperties setObject:@"cnrainbird.com" forKey:NSHTTPCookieDomain];
  [cookieProperties setObject:@"cnrainbird.com" forKey:NSHTTPCookieOriginURL];
  [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
  [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
 
  NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
  [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

  


这样就可以了。输入一下,是不是得到了下面这样的结果:

1
<NSHTTPCookie version:0 name:"username" value:"rainbird" expiresDate:(null) created:2012-03-13 14:36:53 +0000 (3.53342e+08) sessionOnly:TRUE domain:"cnrainbird.com" path:"/" isSecure:FALSE>

其实ios的cookie就这么简单。如果mac也支持这么干的话,那以后写点登陆的脚本不是简单多了:)

 

  • webview设置 cookies

- (void)requestData {
    NSURL *url = [NSURL URLWithString:self.url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];    // 注入Cookie
    [request setValue:[FunctionFast getCookieFromUserDefault] forHTTPHeaderField:@"Cookie"];
    [self.webView loadRequest:request];
}
  • afnetworking设置 cookies

[manager.requestSerializer setValue:[FunctionFast getCookieFromUserDefault] forHTTPHeaderField:@"Cookie"];

  

posted on 2017-08-03 09:40  大醉和尚  阅读(112)  评论(0编辑  收藏  举报