开发者

How to manage Secure cookie with ASIHTTPRequest?

开发者 https://www.devze.com 2023-03-14 11:34 出处:网络
In my ipad project i am using ASIHttpRequest to handle my webservice calls. I am also doing cookie management. My webservice calls are worked fine until i have changed my servic开发者_C百科e cookies a

In my ipad project i am using ASIHttpRequest to handle my webservice calls. I am also doing cookie management. My webservice calls are worked fine until i have changed my servic开发者_C百科e cookies all into Secure one.

How to manage Secure cookie with ASIHTTPRequest? Thanks!


According to the docs:

You can turn off useCookiePersistence, and manage the set of cookies for a particular request manually.

//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:@"https://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]);

To make the cookie secure, just add this to the properties dictionary:

[properties setValue:@"TRUE" forKey:NSHTTPCookieSecure];

Just remember, secure cookies will only be used with HTTPS requests.

0

精彩评论

暂无评论...
验证码 换一张
取 消