You want to call a web service using HTTP POST method, and perhaps pass parameters (as part of the HTTP body or in the query string) to the web service.
Just as with the GET method, we can use the POST method using
NSURLConnection.We must explicitly
set our URL’s method to POST.
Figure 8-3. The POST web service opened in a web browser
This web service expects POST requests and is able to print out parameters that are sent as part of the query string and as part of the HTTP body. You can send both, if you want to. Let’s write a simple app that can create an asynchronous connection and send a few parameters as a query string and a few parameters in the HTTP body to the aforementioned URL:
NSString *urlAsString = @"http://pixolity.com/post.php"; urlAsString = [urlAsString stringByAppendingString:@"?param1=First"]; urlAsString = [urlAsString stringByAppendingString:@"¶m2=Second"]; NSURL *url = [NSURL URLWithString:urlAsString]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [urlRequest setTimeoutInterval:30.0f]; [urlRequest setHTTPMethod:@"POST"]; NSString *body = @"bodyParam1=BodyValue1&bodyParam2=BodyValue2"; ...