iphone原生cookie处理

可以使用NSURLConnection的类来执行HTTP请求,登录该网站,并检索的cookie。 要执行一个请求,只是创建一个NSURLConnection的实例,并分配给它的委托对象。

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURLURLWithString:@"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字符串。 当您要执行另一个请求,将它添加到您对您的NSURLRequest实例的HTTP标头。
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURLURLWithString:@"http://www.google.com/"]];
[request addValue:cookie forHTTPHeaderField:"Cookie"];
NSURLConnection的自动存储和发送的Cookie
原文:http://stackoverflow.com/questions/2053568/managing-http-cookies-on-iphone
原文地址:https://www.cnblogs.com/tomkillua/p/3432099.html