Common network requests have synchronous get, synchronous post, asynchronous get, asynchronous post.
One, synchronous get
1. Initialize the URL to an OC string object nsstring *urlstr = [NSString stringwithformat:@ "%@?query=%@®ion=%@&output=json&ak= 6e823f587c95f0148c19993539b99295 ", Kbusinessinfourl, @" bank ", @" Jinan "]; If Chinese is present in the URL, do urlencode nsstring *newurlstr = [Urlstr stringbyaddingpercentescapesusingencoding: Nsutf8stringencoding]; 2. Build the Network URL object, nsurl nsurl *url = [Nsurl urlwithstring:newurlstr]; 3. Create a network request nsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy: Nsurlrequestreloadignoringlocalcachedata Timeoutinterval:10]; Create a sync link nsurlresponse *response = nil; Nserror *error = nil; NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error];
When you create a sync link, you can parse it in a corresponding way. The same is true for creating asynchronous connections below.
Two, synchronous post // 1. Initialize OC String objects based on URL nsstring *urlstr = [nsstring stringwithformat:@ "%@", kvideourl]; // 2. Creating Nsurl Objects nsurl *url = [nsurl URLWITHSTRING:URLSTR]; // 3. Create Request nsmutableurlrequest *request = [ nsmutableurlrequest requestwithurl:url]; // 4. Creating a parameter String Object nsstring *parmstr = @ "method =album.channel.get&appkey=mykey&format=json&channel=t&pageno=1&pagesize=10 "; // 5. Convert string to NSData object nsdata *pramdata = [ parmstr datausingencoding:nsutf8stringencoding]; // 6. Setting the request body [request sethttpbody:pramdata]; // 7. How to set the request