Sync request, asynchronous request, get request, POST request for iOS

Source: Internet
Author: User

1, the synchronization request can request data from the Internet, once the synchronization request is sent, the program will stop the user interaction until the server returns the data to complete before the next operation,

2, the asynchronous request does not block the main thread, but will establish a new thread to operate, after the user makes an asynchronous request, still can work on the UI, the program can continue to run

3, get request, the parameters are written directly on the access path. Simple operation, but easy to be seen by the outside, security is not high, address up to 255 bytes;

4, POST request, put the parameters inside the body. Post request operation is relatively complex, need to separate parameters and addresses, but high security, parameters placed inside the body, not easily captured.

1, synchronous GET request//The first step is to create the URLNsurl*url = [Nsurl urlwithstring:@"http://api.hudong.com/iphonexml.do?type=focus-c"]; //The second step is to create a network request via a URLnsurlrequest*request = [[Nsurlrequest alloc]initwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy TimeoutInterval:Ten]; //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 contains: Nsurlrequestuseprotocolcachepolicy (Base policy) Nsurlrequestreloadignoringlocalcachedata (suddenly Local cache) Nsurlrequestreturncachedataelseload (the cache is used first, if there is no local cache, it is downloaded from the original address) Nsurlrequestreturncachedatadontload (use local cache , never download, if there is no cache locally, the request fails, and this policy is mostly used for offline operations) Nsurlrequestreloadignoringlocalandremotecachedata (ignoring any caching policies, whether local or remote, Always re-download from the original address) Nsurlrequestreloadrevalidatingcachedata (if the local cache is valid, do not download, and any other cases are re-downloaded from the original address)//step three, connect the serverNSData*received =[nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; NSString*str =[[NSString alloc]initwithdata:received encoding:nsutf8stringencoding]; NSLog (@"%@", str); 2, synchronous POST request//The first step is to create the URLNsurl*url = [Nsurl urlwithstring:@"http://api.hudong.com/iphonexml.do"]; //second step, create the requestnsmutableurlrequest*request = [[Nsmutableurlrequest alloc]initwithurl:url Cachepolicy:nsurlrequestuseprotocolcachepolicy timeOutInterval:Ten]; [Request Sethttpmethod:@"POST"];//Set Request mode to post, default to getNSString*str =@"type=focus-c";//Setting ParametersNSData*data =[str datausingencoding:nsutf8stringencoding];     [Request Sethttpbody:data]; //step three, connect the serverNSData*received =[nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; NSString*STR1 =[[NSString alloc]initwithdata:received encoding:nsutf8stringencoding]; NSLog (@"%@", str1);3, asynchronous get requests//The first step is to create the URLNsurl*url = [Nsurl urlwithstring:@"http://api.hudong.com/iphonexml.do?type=focus-c"]; //second step, create the requestnsurlrequest*request = [[Nsurlrequest alloc]initwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy TimeoutInterval:Ten]; //step three, connect the servernsurlconnection*connection = [[Nsurlconnection alloc]initwithrequest:requestDelegate: self];4, asynchronous POST request//The first step is to create the URLNsurl*url = [Nsurl urlwithstring:@"http://api.hudong.com/iphonexml.do"]; //second step, create the requestnsmutableurlrequest*request = [[Nsmutableurlrequest alloc]initwithurl:url Cachepolicy:nsurlrequestuseprotocolcachepolicy timeOutInterval:Ten]; [Request Sethttpmethod:@"POST"]; NSString*str =@"type=focus-c"; NSData*data =[str datausingencoding:nsutf8stringencoding];     [Request Sethttpbody:data]; //step three, connect the servernsurlconnection*connection = [[Nsurlconnection alloc]initwithrequest:requestDelegate: Self]; 5, the proxy method for the asynchronous request//This method is called when a server response is received- (void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) Response {Nshttpurlresponse*res = (Nshttpurlresponse *) response; NSLog (@"%@", [res allheaderfields]); Self.receivedata=[Nsmutabledata data]; } //called when the server transmits data, this method executes several times according to the data size-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data {[Self.receivedata appenddata:data];}//call this method when the data is passed-(void) Connectiondidfinishloading: (Nsurlconnection *) connection {nsstring*receivestr =[[NSString Alloc]initwithdata:self.receivedata encoding:nsutf8stringencoding]; NSLog (@"%@", RECEIVESTR); } //During a network request, any errors (network disconnection, connection timeouts, etc.) will enter this method-(void) Connection: (Nsurlconnection *) connection Didfailwitherror: (Nserror*) Error {NSLog (@"%@", [error localizeddescription]);

Ext.: http://www.open-open.com/lib/view/open1355055986679.html

Sync request, asynchronous request, get request, POST request for iOS

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.