ASIHttpRequest使用详解(4)
2013-09-26 15:06
716 查看
一、Cookie的使用
持久化cookie
ASIHTTPRequest允许你使用全局存储来和所有使用CFNetwork或者NSURLRequest接口的程序共享cookie。如果设置useCookiePersistence为YES(默认值),cookie会被存储在共享的 NSHTTPCookieStorage 容器中,并且会自动被其他request重用。
ASIHTTPRequest会向服务器发送其他程序创建的cookie(如果这些cookie对特定request有效的话)。
你可以清空session期间创建的所有cookie
[ASIHTTPRequest setSessionCookies:nil];
这里的‘session cookies’指的是一个session中创建的所有cookie,而非没有过期时间的cookie(即通常所指的会话cookie,这种cookie会在程序结束时被清除)。
另外,有个方便的函数 clearSession可以清除session期间产生的所有的cookie和缓存的授权数据。
[ASIHTTPRequest clearSession];
自己处理cookie
如果你愿意,你大可以关闭useCookiePersistence,自己来管理某个request的一系列cookie//Create a cookie NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease]; [properties setValue:[@"Test Value" encodedCookieValue] forKey:NSHTTPCookieValue]; [properties setValue:@"ASIHTTPRequestTestCookie" forKey:NSHTTPCookieName]; [properties setValue:@".allseeing-i.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]; //This url will return the value of the 'ASIHTTPRequestTestCookie' cookie url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"]; request = [ASIHTTPRequest requestWithURL:url]; [request setUseCookiePersistence:NO]; [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]]; [request startSynchronous]; //Should be: I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie' NSLog(@"%@",[request responseString]);
相关文章推荐
- ASIHTTPRequest 简介/使用详解
- ASIHTTPRequest 使用详解
- asihttprequest和解压缩简单使用详解
- ASIHTTPRequest使用详解
- ASIHttprequest使用详解
- IOS ASIHttpRequest使用详解
- ASIHttprequest使用详解
- ASIHttpRequest使用详解(1)
- ASIHttpRequest使用详解
- ASIHttpRequest使用详解(3)
- ASIHttpRequest网络请求第三方类库使用方法详解
- IOS中使用ASIHTTPRequest中https请求详解
- ASIHTTPRequest 详解
- ASIHttpRequest 详解[转载]
- ASIHTTPRequest简单使用
- iOS学习之-使用ASIHttpRequest调用WebService
- IOS学习十四:网络之ASIHttpRequest和SBJson简单使用
- ASIHTTPRequest 详解, 够详细 - -
- ASIHTTPRequest详解 [经典3]
- iOS ASIHTTPRequest详解