7. ASIHTTPRequest-Cookie的使用

持久化cookie

ASIHTTPRequest容许你使用全局存储来和全部使用CFNetwork或者NSURLRequest接口的程序共享cookie。 缓存

若是设置useCookiePersistence为YES(默认值),cookie会被存储在共享的 NSHTTPCookieStorage 容器中,而且会自动被其余request重用。值得一提的是,ASIHTTPRequest会向服务器发送其余程序建立的cookie(若是这些cookie对特定request有效的话)。 服务器

你能够清空session期间建立的全部cookie: cookie

[ASIHTTPRequest setSessionCookies:nil];

这里的‘session cookies’指的是一个session中建立的全部cookie,而非没有过时时间的cookie(即一般所指的会话cookie,这种cookie会在程序结束时被清除)。 session

另外,有个方便的函数 clearSession能够清除session期间产生的全部的cookie和缓存的受权数据。  函数

 

本身处理cookie

若是你愿意,你大能够关闭useCookiePersistence,本身来管理某个request的一系列cookie: url

//建立一个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]);
相关文章
相关标签/搜索