Fork me on GitHub

在iPhone中保存Cookies的方法

NSURLRequest*request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
NSURLConnection*connection =[NSURLConnection connectionWithRequest:request delegate:self];
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
   
NSHTTPURLResponse*HTTPResponse=(NSHTTPURLResponse*)response;
   
NSDictionary*fields =[HTTPResponse allHeaderFields];
   
NSString*cookie =[fields valueForKey:"Set-Cookie"];// It is your cookie
}
保存好Cookie,当另一个request要用到这个cookies时,用如下方法加入:
NSMutableURLRequest*request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
[request addValue:cookie forHTTPHeaderField:"Cookie"];

posted on 2012-02-16 15:38  pengyingh  阅读(870)  评论(0编辑  收藏  举报

导航