iphone开发 cookie处理

本文介绍如何使用NSURLConnection类发起HTTP请求进行网站登录,并获取Cookie。包括创建NSURLConnection实例、实现委托方法解析响应头字段提取Cookie字符串。此外,提供了iOS设备上管理和打印Cookie的方法,以及如何在后续请求中利用Cookie。最后,展示了设置和读取Cookie的代码示例。

You can use the NSURLConnection class to perform a HTTP request to login the website, and retrieve the cookie. To perform a request, just create an instance of NSURLConnection and assign a delegate object to it.

1 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
2 NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

Then, implement a delegate method.

1 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
2 {
3 NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
4 NSDictionary *fields = [HTTPResponse allHeaderFields];
5 NSString *cookie = [fields valueForKey:"Set-Cookie"]; // It is your cookie
6 }

Retain or copy the cookie string. When you want to perform another request, add it to your HTTP header of your NSURLRequest instance.

1 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
2 [request addValue:cookie forHTTPHeaderField:"Cookie"];

ps:

You don't need to do this, NSURLConnection automatically stores and sends cookies unless you explicitly tell it not to. – benzado Apr 20 '10 at 15:33
Even though this shouldn't be necessary fixed a suspected bug in iOS 4.2GM – crackity_jones Nov 12 '10 at 0:09

 

 

转:http://stackoverflow.com/questions/2053568/managing-http-cookies-on-iphone


下面为:iphone 设置cookie 打印cookie方法

 1 int i;
2
3 NSDictionary *dic = [NSDictionary dictionaryWithObject:@"name=value; path=/; domain=.test.com;, name2=value2; path=/;" forKey:@"Set-Cookie"];
4
5 NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:dic forURL:[NSURL URLWithString:@"http://www.foo.com/"]];
6
7
8
9 for (i = 0; i < [cookies count]; i++)
10
11 {
12
13 NSHTTPCookie *cookie = [cookies objectAtIndex:i];
14
15 NSLog(@"%@ for %@",[cookie name], [cookie domain]);
16
17 }
18
19 for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
20
21 {
22
23 NSLog(@"name: '%@'\n", [cookie name]);
24
25 NSLog(@"value: '%@'\n", [cookie value]);
26
27 NSLog(@"domain: '%@'\n", [cookie domain]);
28
29 }
30
31 /////////////////////////////////////////////////////////////////////////////////////////////////
32
33 NSURL *cookieURL = [NSURL URLWithString:@"http://www.tudou.com"];
34
35 //read
36 NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:cookieURL];
37
38 //creat a new cookie
39 NSArray *newCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[NSDictionary dictionaryWithObject:@"yourKey=yourValue;expires=2100-01-11 11:11:11 +0800" forKey:@"Set-Cookie"] forURL:cookieURL];
40
41 //wirte
42 [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:newCookies forURL:cookieURL mainDocumentURL:nil];

代码调试通过,具体可以参看Xcode Document

-(void)writeCookie{

NSArray *keys = [NSArray arrayWithObjects:

NSHTTPCookieSecure,

NSHTTPCookieDomain,

NSHTTPCookieExpires,

NSHTTPCookieName,

NSHTTPCookiePath,

NSHTTPCookieValue,nil];

NSArray *objects = [NSArray arrayWithObjects:

@"TRUE",

@".202.153.106.234",

nil, //[[NSDate date] initWithTimeIntervalSinceNow:86500],

@"SPM_auth",

@"/spwebapi/",

@"FeiXiaoLin",nil];

NSDictionary *dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSHTTPCookie *cookieA = [NSHTTPCookie cookieWithProperties:dict];

NSHTTPCookieStorage *sharedHTTPCookie = [NSHTTPCookieStorage sharedHTTPCookieStorage];

[sharedHTTPCookie setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

[sharedHTTPCookie setCookie:cookieA];

NSLog(@"写入后:%@",[[NSHTTPCookieStorage sharedHTTPCookieStorage]cookies]);

}





-(void)readCookie {

// NSHTTPCookie *cookieB;

// NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

NSLog(@"cookies:%@",[[NSHTTPCookieStorage sharedHTTPCookieStorage]cookies]);

}





转载于:https://www.cnblogs.com/gtts/archive/2012/02/12/2348367.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值