如何使用ASIHTTPRequest管理安全cookie?

时间:2011-06-22 11:20:40

标签: iphone objective-c asihttprequest

在我的ipad项目中,我使用ASIHttpRequest来处理我的webservice调用。我也在做cookie管理。我的网络服务电话工作正常,直到我将我的服务cookie全部更改为Secure one。

如何使用ASIHTTPRequest管理安全cookie? 谢谢!

1 个答案:

答案 0 :(得分:0)

根据docs

  

您可以关闭useCookiePersistence,并手动管理特定请求的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:@"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]);

要使cookie安全,只需将其添加到属性字典中:

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

请记住,安全cookie只能用于HTTPS请求。