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 URL
Nsurl *url = [Nsurl urlwithstring:@ "http://api.hudong.com/iphonexml.do?type=focus-c"];
The second step is to create a network request via 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 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)
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
//First step, create URL
nsurl *url = [nsurl urlwithstring:@ "/http/ Api.hudong.com/iphonexml.do "];
//second step, create request
nsmutableurlrequest *request = [[Nsmutableurlrequest alloc] Initwithurl:url Cachepolicy:nsurlrequestuseprotocolcachepolicy Timeoutinterval:10];
[Request sethttpmethod:@ "POST"];//provisioning is post, default is Get
nsstring *str = @ " Type=focus-c ";//Set Parameters
nsdata *data = [str datausingencoding:nsutf8stringencoding];
[Request Sethttpbody:data];
//Third step, connecting server
nsdata *received = [nsurlconnection Sendsynchronousrequest:request Returningresponse:nil Error:nil];
nsstring *str1 = [[NSString alloc]initwithdata:received encoding: Nsutf8stringencoding];
NSLog (@ "%@", str1);
3, asynchronous GET request
//First step, create URL
nsurl *url = [nsurl urlwithstring:@ "/http/ Api.hudong.com/iphonexml.do?type=focus-c "];
//second step, create request
nsurlrequest *request = [[Nsurlrequest alloc]initwithurl:url Cachepolicy:nsurlrequestuseprotocolcachepolicy Timeoutinterval:10];
//Third step, connecting server
nsurlconnection *connection = [[Nsurlconnection alloc] Initwithrequest:request Delegate:self];
4, asynchronous POST request
//First step, create URL
nsurl *url = [nsurl urlwithstring:@ "/http/ Api.hudong.com/iphonexml.do "];
//second step, create 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];
//Third step, connecting server
nsurlconnection *connection = [[Nsurlconnection alloc] Initwithrequest:request Delegate:self];
5, Proxy method for asynchronous request
//Call this method when receiving a server response
-(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]);
}