How to get correct Set-Cookie headers for NSHTTPURLResponse?

Posted by overboming on Stack Overflow See other posts from Stack Overflow or by overboming
Published on 2010-03-23T14:10:31Z Indexed on 2010/03/23 14:13 UTC
Read the original article Hit count: 672

Filed under:
|
|
|

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?

NSMutableURLRequest *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]);

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about nsurlconnection