Hello,
I face some problems about the url POST method and the cookies
The program requires the users to enter 2 parameters into the UItextField. And then the user has to click a button to submit the data through the url POST method and then the cookies of the website will be return to the application. There are some http 302(redirect) or 303 in some of the website. The url is a 'https', not 'http'.
I have complete the GET method to some websites. However, there are some problems when I use the POST method.
The following is the coding of my program.
I have not idea about the error.
I put the code within the viewController.m, - (IBAction)pressLoginButton
id)sender
Can any help me?
Thank you very much.
I face some problems about the url POST method and the cookies
The program requires the users to enter 2 parameters into the UItextField. And then the user has to click a button to submit the data through the url POST method and then the cookies of the website will be return to the application. There are some http 302(redirect) or 303 in some of the website. The url is a 'https', not 'http'.
I have complete the GET method to some websites. However, there are some problems when I use the POST method.
The following is the coding of my program.
Code:
// GET method
NSString *urlString = [NSString stringWithFormat:@"http://google.com/"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *results = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
// POST method
NSString *post = [NSString stringWithFormat:@"username=%@&password=%@", textFieldUsername.text, textFieldPassword.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"https://url"]]; // the url is the website url, https
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
I have not idea about the error.
I put the code within the viewController.m, - (IBAction)pressLoginButton
Can any help me?
Thank you very much.