Request method in Network request 4

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.