Wednesday, May 1, 2013

NSMutableURLRequest setHTTPShouldHandleCookies example ios


setHTTPShouldHandleCookies:

Sets whether the receiver should use the default cookie handling for the request.
- (void)setHTTPShouldHandleCookies:(BOOL)handleCookies
Parameters of [NSMutableURLRequest setHTTPShouldHandleCookies]
handleCookies
YES if the receiver should use the default cookie handling for the request, NO otherwise. The default is YES.
Special Considerations of [NSMutableURLRequest setHTTPShouldHandleCookies]
In OS X v10.2 with Safari 1.0 the value set by this method is not respected by the framework.
Example of [NSMutableURLRequest setHTTPShouldHandleCookies]
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:WAF_URL] 
                                                       cachePolicy:NSURLRequestReloadIgnoringCacheData 
                                                   timeoutInterval:60];
[request setHTTPShouldHandleCookies:NO];
self.conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
Example of [NSMutableURLRequest setHTTPShouldHandleCookies]
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]);