Implementation of iOS sync GET request

Source: Internet
Author: User

The first step is to create the URL

Nsurl *url = [Nsurl urlwithstring:@ "Http://itunes.apple.com/search?term=&entity=software"];

The second step is to create a network request via a URL

Nsurlrequest *request = [[Nsurlrequest alloc]initwithurl:url Cachepolicy:nsurlrequestuseprotocolcachepolicy TIMEOUTINTERVAL:10];

Step three, connect the server

NSData *received = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];

NSString *str = [[NSString alloc]initwithdata:received encoding:nsutf8stringencoding];

NSLog (@ "%@", str); It's so simple, it's done here, and Str is the result of the request.

Note: The parameters in the second step are explained as follows:

//nsurlrequest Initialization method first parameter: Request access path, second parameter: Cache protocol, third parameter: Network request time-out (seconds)

// where the cache protocol is an enumeration type that contains:

nsurlrequestuseprotocolcachepolicy//(Basic strategy)

nsurlrequestreloadignoringlocalcachedata//(ignoring local cache)

nsurlrequestreturncachedataelseload//(First use cache, if there is no local cache, download from the original address)

nsurlrequestreturncachedatadontload//(use local cache, never download, if there is no cache locally, the request fails, this policy is used for offline operations) www.2cto.com

nsurlrequestreloadignoringlocalandremotecachedata//(ignoring any caching policies, whether local or remote, always re-downloaded from the original address)

nsurlrequestreloadrevalidatingcachedata//(does not download if the local cache is valid, and any other cases are re-downloaded from the original address)

Implementation of iOS sync GET request

Related Article

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.