如何使用ASIHTTPRequest管理安全cookie



在我的ipad项目中,我使用ASIHttpRequest来处理我的Web服务调用。我也在做饼干管理。我的网络服务呼叫运行良好,直到我将服务cookie全部更改为安全cookie。

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

根据文档:

您可以关闭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请求。

最新更新