开发者

How to get correct Set-Cookie headers for NSHTTPURLResponse?

I want to use the following code to login to a website which returns its cookie information in the following manner:

Set-Cookie: 19231234
Set-Cookie: u2am1342340
Set-Cookie: owwjera

I'm using the following code to log in to the site, but the print statement at the end doesn't output anything about "set-cookie". On Snow leopard, the library seems to automatically pick up the cookie for this site and later connections sent out is set with correct "cookie" headers. But on leopard, it doesn't work that way, so is that a trigger for this "remember the cookie for certain root url" behavior?

NSMutableURLRequ开发者_StackOverflowest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:uurl]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"keep-live" forHTTPHeaderField:@"Connection"];
[request setValue:@"300" forHTTPHeaderField:@"Keep-Alive"];
[request setHTTPShouldHandleCookies:YES];

[request setHTTPBody:postData];
[request setTimeoutInterval:10.0];

NSData *urlData;
NSHTTPURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:request
                                returningResponse:&response
                                            error:&error];

NSLog(@"response dictionary %@",[response allHeaderFields]);


[request setHTTPShouldHandleCookies:YES];

This line causes the system to handle cookies for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜