iphone开发 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]);

}





你可能感兴趣的:(iPhone开发)