9.5. Sending HTTP GET Requests with NSURLConnection

Problem

You want to send a GET request over the HTTP protocol and perhaps pass parameters along your request to the receiver.

Solution

By convention, GET requests allow parameters through query strings of the familiar form:

http://example.com/?param1=value1&param2=value2...

You can use strings to provide the parameters in the conventional format.

Discussion

A GET request is a request to a web server to retrieve data. The request usually carries some parameters, which are sent in a query string as part of the URL.

To let you test parameter passing, I have prepared a simple GET web service at the following address: http://pixolity.com/get.php. If you open this URL in your browser, you will see something similar to Figure 9-1.

The example GET web service opened in a web browser

Figure 9-1. The example GET web service opened in a web browser

So our web browser is able to open this URL just fine, and you can see that the web service is able to detect query string parameters and GET parameters. Now if you open the following URL (http://pixolity.com/get.php?param1=First&param2=Second) in your browser, you will see results similar to those shown in Figure 9-2.

Query string parameters sent to the GET web service

Figure 9-2. Query string parameters sent to the GET web service

To simulate sending query string parameters in a GET request to the same web service ...

Get iOS 6 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.