因为使用UIWebView必不可少的肯定会用到cookie,在同一个应用里面,会在沙盒的lib文件夹下面生成一个cookie的文件夹,这样可以实现cookie的通用了。。。。
持久化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]);
————————————————————————————————————————————————————————————————
还是不爽的话,那就看下面的方法吧
项目中,需要在打开3g网页时,通过cookie传递一些信息。
实现代码如下:
在
- NSURL *url = [NSURL URLWithString:_urlstr];
- //NSURLRequest *request = [NSURLRequest requestWithURL:url];
- NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
- [self.myWeb loadRequest:request];
之前,设置或者删除cookie。
- //////////////////////////////////////////////////////
- //设置cookie
- - (void)setCookie{
-
-
- NSMutableDictionary *cookiePropertiesUser = [NSMutableDictionary dictionary];
- [cookiePropertiesUser setObject:@"cookie_user" forKey:NSHTTPCookieName];
- [cookiePropertiesUser setObject:uid forKey:NSHTTPCookieValue];
- [cookiePropertiesUser setObject:@"xxx.xxx.com" forKey:NSHTTPCookieDomain];
- [cookiePropertiesUser setObject:@"/" forKey:NSHTTPCookiePath];
- [cookiePropertiesUser setObject:@"0" forKey:NSHTTPCookieVersion];
-
- // set expiration to one month from now or any NSDate of your choosing
- // this makes the cookie sessionless and it will persist across web sessions and app launches
- /// if you want the cookie to be destroyed when your app exits, don‘t set this
- [cookiePropertiesUser setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
-
- NSHTTPCookie *cookieuser = [NSHTTPCookie cookieWithProperties:cookiePropertiesUser];
- [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookieuser];
- }
-
- //清除cookie
- - (void)deleteCookie{
- NSHTTPCookie *cookie;
-
- NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
-
- NSArray *cookieAry = [cookieJar cookiesForURL: [NSURL URLWithString: _urlstr]];
-
- for (cookie in cookieAry) {
-
- [cookieJar deleteCookie: cookie];
-
- }
- }