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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//创建一个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]);

ASIHTTPRequest-Cookie的使用[转]的更多相关文章

  1. ios htttp网络请求cookie的读取与写入(NSHTTPCookieStorage)

    当你访问一个网站时,NSURLRequest都会帮你主动记录下来你访问的站点设置的Cookie,如果 Cookie 存在的话,会把这些信息放在 NSHTTPCookieStorage 容器中共享,当你 ...

  2. 强大的HTTP包装开源项目ASIHTTPRequest介绍

    ASIHTTPRequest 是一个直接在CFNetwork上做的开源项目,提供了一个比官方更方便更强大的HTTP网络传输的封装.它的特色功能如下: 1,下载的数据直接保存到内存或文件系统里 2,提供 ...

  3. ASIHTTPRequest详解 [经典]

    ASIHTTPRequest 对CFNetwork API进行了封装,并且使用起来非常简单,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中.ASIHTTPR ...

  4. ASIHTTPRequest类库简介和使用说明

    官方网站: http://allseeing-i.com/ASIHTTPRequest/ .可以从上面下载到最新源码,以及获取到相关的资料. 使用iOS SDK中的HTTP网络请求API,相当的复杂, ...

  5. IOS 网络请求中设置cookie

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   1. ASIHTTPRequest ASIHTTPRequest 是一款极其强劲的 HTTP 访问开源项目.让简单的 A ...

  6. iOS - ASIHTTPRequest 网络请求

    前言 使用 iOS SDK 中的 HTTP 网络请求 API,相当的复杂,调用很繁琐,ASIHTTPRequest 就是一个对 CFNetwork API 进行了封装,并且使用起来非常简单的一套 AP ...

  7. IOS 在http请求中使用cookie

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainbird.blog.51cto.com/211214/805173 一直以 ...

  8. ASIHTTPRequest详解

    ASIHTTPRequest对CFNetwork API进行了封装,并且使用起来非常简单,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中.ASIHTTPRe ...

  9. ASIHTTPREQUEST 文档

    http://blog.csdn.net/ysysbaobei/article/details/17026577 Please note that I am no longer working on ...

  10. IOS网络请求框架AFNetworking和ASIHttpRequest对比

    ASI基于CFNetwork框架开发,而AFN基于NSURL. ASI更底层,请求使用创建CFHTTPMessageRef进行,使用NSOperationQueue进行管理,ASIHTTPReques ...

随机推荐

  1. 关于Eclipse

    Navigator窗口 之前看到同事使用Eclipse的Navigator窗口,十分不解这个窗口有啥用:今天通过了解才知道Package Explorer是从工程的角度来显示文件,比如settings ...

  2. 显示列表控件(引用SourceGrid)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...

  3. 善待Erlang 代码 -- Xref 实践

    Xref 是一个交叉引用工具,通过分析定义的函数间的调用关系,用于查找函数. 模块. 应用程序和版本之间的依赖关系. 通俗而言,Xref 可以检查代码中函数的调用关系.比如在 moduleA 中的 f ...

  4. java 面向对象 — 封装

      

  5. HDU 1251 统计难题(字典树)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  6. [转][Java]简单标签库简介

    public class SimpleTagDemo extends SimpleTagSupport { @Override public void doTag() throws JspExcept ...

  7. Request模块(八)

    Requests: 让 HTTP 服务人类 虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 “ ...

  8. ImportError: Couldn't import Django.或者提示Django 模块不存在

    ImportError: Couldn't import Django. 或者 多版本的python引起的,执行以下命令  即可解决问题 python3是新的版本的python python3 -m ...

  9. YII cookie和session的使用方法

    设置cookie://首先新建cookie$cookie = new CHttpCookie(‘mycookie’, ‘this is my cookie’);//定义cookie的有效期$cooki ...

  10. Vue 实现微信 jssdk 扫码, 上传图片

    流程 1: 配置微信公众号JS域名 2:前端发送URL后台获取JSSDK配置, 后台Service代码如下, 修改2处位置: WeixinUtil.APPID => 当前公众号APPID Wei ...