First of all to introduce the next iOS synchronization request, asynchronous request, get request, the meaning of post, and then in one by one to introduce the example.
1, the synchronization request can request the data from the Internet, once sends the synchronization request, the program will stop the user interaction, until the server returns the data completes, only then may carry on the next operation,
2, the asynchronous request will not block the main thread, but will create a new thread to operate, after the user issued an asynchronous request, the UI can still operate, the program can continue to run
3, GET request, write the parameter directly on the access path. Simple to operate, but easy to be seen outside, security is not high, address up to 255 bytes;
4, POST request, put the parameters into the body inside. Post request operation is relatively complex, need to separate parameters and addresses, but high security, the parameters are placed inside the body, not easily captured.
1. Sync GET request
/First step, create URL nsurl *url = [Nsurl urlwithstring:@] http://api.hudong.com/iphonexml.do?
Type=focus-c "]; The second step is to create a network request through a URL nsurlrequest *request = [[Nsurlrequest alloc]initwithurl:url CachePolicy:
Nsurlrequestuseprotocolcachepolicy Timeoutinterval:10];
Nsurlrequest initialization Method first parameter: Request access path, second parameter: Cache protocol, third parameter: Network Request timeout (sec) where the cache protocol is an enumeration type contains:
Nsurlrequestuseprotocolcachepolicy (underlying policy) Nsurlrequestreloadignoringlocalcachedata (ignore local cache) Nsurlrequestreturncachedataelseload (use caching first, download from the original address if there is no local cache) Nsurlrequestreturncachedatadontload (use local cache, never download,
If there is no cache locally, the request fails, and this policy is used for offline operations Nsurlrequestreloadignoringlocalandremotecachedata (regardless of any caching policy, whether local or remote, always download from the original address) Nsurlrequestreloadrevalidatingcachedata (if the local cache is valid, no download, all other cases from the original address)//step three, connect the server NSData *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 URL
nsurl *url = [Nsurl urlwithstring:@ "http://api.hudong.com/iphonexml.do"];
The second step is to create the request
nsmutableurlrequest *request = [[Nsmutableurlrequest alloc]initwithurl:url CachePolicy: Nsurlrequestuseprotocolcachepolicy Timeoutinterval:10];
[Request sethttpmethod:@ ' Post '];//set the requested mode to post, the default is get
nsstring *str = @ "type=focus-c";//Set parameter
nsdata *data = [ STR datausingencoding:nsutf8stringencoding];
[Request Sethttpbody:data];
Step three, connect the server
nsdata *received = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil] ;
NSString *STR1 = [[NSString alloc]initwithdata:received encoding:nsutf8stringencoding];
NSLog (@ "%@", str1);
3. Asynchronous GET Request
The first step is to create the URL
nsurl *url = [Nsurl urlwithstring:@ "Http://api.hudong.com/iphonexml.do?type=focus-c"];
The second step is to create the request
nsurlrequest *request = [[Nsurlrequest alloc]initwithurl:url CachePolicy: Nsurlrequestuseprotocolcachepolicy Timeoutinterval:10];
The third step is to connect the server
nsurlconnection *connection = [[Nsurlconnection alloc]initwithrequest:request delegate:self];
4. Asynchronous POST request
The first step is to create the URL
nsurl *url = [Nsurl urlwithstring:@ "http://api.hudong.com/iphonexml.do"];
The second step is to create the request
nsmutableurlrequest *request = [[Nsmutableurlrequest alloc]initwithurl:url CachePolicy: Nsurlrequestuseprotocolcachepolicy Timeoutinterval:10];
[Request sethttpmethod:@ "POST"];
NSString *str = @ "Type=focus-c";
NSData *data = [str datausingencoding:nsutf8stringencoding];
[Request Sethttpbody:data];
The third step is to connect the server
nsurlconnection *connection = [[Nsurlconnection alloc]initwithrequest:request delegate:self];
5. Proxy method for asynchronous requests
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 based on the size of the data
-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data
{
[self.receivedata appenddata:data];
}
This method is called after 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 (disconnection, connection timeout, etc.) enter this method
-(void) connection: (Nsurlconnection *) connection
Didfailwitherror: ( Nserror *) Error
{
NSLog (@ "%@", [Error localizeddescription]);
The above is a small set of iOS for you to introduce synchronous request, asynchronous request, get request, POST request comprehensive analysis, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!