Reputation: 473
In my program I call two functions, the first a a login, the second one a function to parse data.
To store session, with the login function I save my cookies with this:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
if(connection == conn_login){
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSDictionary *fields = [HTTPResponse allHeaderFields];
cookie = [fields valueForKey:@"Set-Cookie"];
}
}
Everything is fine, when I print cookie it is:
userid=1; expires=Mon, 05-Aug-2013 19:22:18 GMT; path=/; domain=www.mydomain.com
"userid=1" is what I'm interested in.
So in the following NSURLRequest where I parse the data, I do like this to set the saved cookie:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL
URLWithString:@"http://www.mydomain.com/?getList"]];
[request setValue:cookie forHTTPHeaderField:@"Set-Cookie"];
But it doesn't work. My previous cookies are not set, and even if a print the complete header of the 2nd request, there is no "userid" or whatsoever.
What am I doing wrong? Thank you all.
Upvotes: 0
Views: 2784
Reputation: 21
I think you have to use this line of code... If you have doubts let me know
NSArray *cookies1 = [[ NSHTTPCookieStorage sharedHTTPCookieStorage] cookies ];
NSDictionary *cookieHeaders;
cookieHeaders = [ NSHTTPCookie requestHeaderFieldsWithCookies: cookies1];
[request setValue: [cookieHeaders objectForKey: @"Cookie" ]forHTTPHeaderField:@"Cookie" ];
Upvotes: 1