bamya
bamya

Reputation: 416

How to make second request on NSMutableURLRequest?

I am trying to make a connection with session by using NSMutableURLRequest. I am starting the first request I receive data from it. But I can't make the second request. How can I make the second request by using the same cookie?

Update source code:

NSHTTPURLResponse   * response;
NSError             * error;
NSMutableURLRequest * request;
request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://loginsite"]
                                        cachePolicy:NSURLRequestReloadIgnoringCacheData
                                    timeoutInterval:60];
NSString *post = [NSString stringWithFormat:@"userid=%@&pass=%@",self.userName.text, self.password.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.urlConnection start];

it makes the first request but how can I make the second one?

Upvotes: 0

Views: 110

Answers (1)

Daij-Djan
Daij-Djan

Reputation: 50139

Cookies are an HTTP thingy so I assume we are talking about that.
When the 1. request succeeds you get a NSURLResponse which is an HTTPURLResponse with headers that can be resolved to cookies:

NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:headers forURL:[self url]]];

those cookies can then be used with the 2. request...

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[request setAllHTTPHeaderFields:headers];

Upvotes: 1

Related Questions