POST request using Cocoa under iOS

9
Oct
1

I’m digging into iPhone proramming API and I was needing to execute a POST operation. At the beggining I was thinking that I had to make all the request details but fortunetly there is a pretty straigh way of doing it… take a look:


NSURL *url = [NSURL URLWithString:@"http://www.google.com.br"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSData *requestBody = [@"username:x&password:y" dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:requestBody];
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];

Filed under: Photography
1 Comment

1 Comment

Sorry, the comment form is closed at this time.