11.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¶m2=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.
In order to test a GET call, you need to find a web server that accepts the GET method and can send you some data back. This is simple. You may already know that when you open a web page in your browser, your browser by default sends a GET request to that end point, so you can use this recipe on any website of your liking.
To simulate sending query string parameters in a GET request
to the same web service using NSURLConnection, use a mutable URL request and
explicitly specify your HTTP method to GET using the setHTTPMethod: method of NSMutableURLRequest and put your parameters as
part of the URL, like so:
-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{NSString*urlAsString=<#PlacetheURLofthewebserverhere#>;urlAsString=[urlAsStringstringByAppendingString:@"?param1=First"];urlAsString=[urlAsStringstringByAppendingString:@"¶m2=Second"];NSURL
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access