Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MarkSiu

macrumors newbie
Original poster
Jun 27, 2010
3
0
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.

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:(id)sender
Can any help me?
Thank you very much.
 
Set up a delegate for your NSURLConnection and log the error it's returning.

Thank you for your reply.
Do you mean this?

Code:
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

After typing the above code, I cannot get the code back.
However, I faces another big problems.

Before enter to the url in my first post. I have to use a GET method go to a website first and get the cookies.

It means that the flow is
Website X (GET method, get the cookies) --> Website Y (The post url, POST method, to get the user enter parameters)
--> 302/303 (have to store all the cookies during the redirection of http status) --> 200 and have store all the cookies

Btw, what is delegate the NSURLConnection? Is there are any relationship between the NSMutableURLRequest and NSURLConnection?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.