博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASIHTTPRequest-Cookie的使用
阅读量:4570 次
发布时间:2019-06-08

本文共 2996 字,大约阅读时间需要 9 分钟。

 本文转载至 http://www.cocoachina.com/bbs/read.php?tid=93220&page=e&#a

 
 
持久化cookie
ASIHTTPRequest允许你使用全局存储来和所有使用CFNetwork或者NSURLRequest接口的程序共享cookie。
如果设置useCookiePersistence为YES(默认值),cookie会被存储在共享的 NSHTTPCookieStorage 容器中,并且会自动被其他request重用。值得一提的是,ASIHTTPRequest会向服务器发送其他程序创建的cookie(如果这些cookie对特定request有效的话)。
你可以清空session期间创建的所有cookie:
1
    
[ASIHTTPRequest setSessionCookies:nil];
这里的‘session cookies’指的是一个session中创建的所有cookie,而非没有过期时间的cookie(即通常所指的会话cookie,这种cookie会在程序结束时被清除)。
另外,有个方便的函数 clearSession可以清除session期间产生的所有的cookie和缓存的授权数据。 
自己处理cookie
如果你愿意,你大可以关闭useCookiePersistence,自己来管理某个request的一系列cookie:
//创建一个cookie
NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
[properties setValue:[@"Test Value" encodedCookieValue] forKey:NSHTTPCookieValue];
[properties setValue:@"ASIHTTPRequestTestCookie" forKey:NSHTTPCookieName];
[properties setValue:@".dreamingwish.com" forKey:NSHTTPCookieDomain];
[properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
[properties setValue:@"/asi-http-request/tests" forKey:NSHTTPCookiePath];
NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
//这个url会返回名为'ASIHTTPRequestTestCookie'的cookie的值
url = [NSURL URLWithString:@"http://www.dreamingwish.com/"];
request = [ASIHTTPRequest requestWithURL:url];
[request setUseCookiePersistence:NO];
[request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];
[request startSynchronous];
//将会打印: I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie'
NSLog(@"%@",[request responseString]);
 
 
 
本文转载至 http://www.colabug.com/thread-1051112-1-1.html
最近项目中要用Cookie来实现登录,之前的项目都是保存的帐号和加密过后的密码,对这块不是很了解。Cookie登录就是判断本地有没有Cookie有的话直接登陆,项目里的一些接口在请求中加上自定义的Cookie。项目使用的是asigttprequest这个开源框架。 
  后台登录的实现需要2个过程,一是保存Cookie,二是上传Cookie。 
  在第一次登录时要保存下Cookie的Token,这个是要需要和后台做好约定的。第一次登录时会返给你后台的Cookie信息, 
  [request responseCookies]方法可以得到Cookie数组的信息, 
  NSDictionary *headers = [request responseHeaders]; 
  NSString *setcookie = [headers objectForKey:@"Set-Cookie"] ; 
这2个方法可以得到你想要的token.在得到的token字符串中分割时你想要的信息。  这时利用NSUserDefaults把token保存下来。方便后面使用cookie登录。 
  接下来的工作是上传保存下来的cookie,在asihttprequest文档上也有说明。 
  代码如下: 
  1. NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
  2. [properties setValue:[NSString stringWithFormat:@"%@",[GUserSetting provider].UserCookie] forKey:NSHTTPCookieValue];
  3. [properties setValue:@"SessionId" forKey:NSHTTPCookieName];
  4. [properties setValue:domain forKey:NSHTTPCookieDomain];
  5. [properties setValue:@"/" forKey:NSHTTPCookiePath];
  6. NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
  7. //NSLog(@"%@",cookie);
  8. [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];
这里需要注意的是, 
  NSHTTPCookieName是和服务器约定好的token, 
  NSHTTPCookieValue是你要刚才保存下来的token值, 
  NSHTTPCookieDomain和NSHTTPCookiePath一定要加上,不然会上传不成功的Cookie。 
  在asihttrequest时使用时要把setUseCookiePersistence设为NO; 
  我这样我们在用Cookie登录时只要把把保存下的Cookie上传到服务器就可以正常使用接口了,不会提示未登录。
 

转载于:https://www.cnblogs.com/Camier-myNiuer/p/3683805.html

你可能感兴趣的文章
阻止putty变成inactive
查看>>
TP框架代码学习 学习记录 3.2.3
查看>>
doc文档生成带目录的pdf文件方法
查看>>
js数组,在遍历中删除元素(用 for (var i in arr)是无效的 )
查看>>
通过前端上传图片等文件的方法
查看>>
在 OC 中调用 Swift 代码
查看>>
Android仿腾讯应用宝 应用市场,下载界面, 有了进展button
查看>>
安卓|五大逆向软件下载
查看>>
5 OK6410裸机调试(不用Jlink)
查看>>
“模板”学习笔记(5)-----编译器在处理函数模板的时候都干了啥
查看>>
教你用shell写CGI程序
查看>>
窗口 对话框 Pop Dialog 示例
查看>>
ubuntu(centos) server安装vmware tools
查看>>
数据结构之最大不重复串
查看>>
为什么要配置sdk-tools/platform-toools?
查看>>
自己动手开发更好用的markdown编辑器-07(扩展语法)
查看>>
maven dependency:tree中反斜杠的含义
查看>>
队列的循环队列
查看>>
程序中的日期格式
查看>>
大众点评CAT错误总结以及解决思路
查看>>