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

Source: Internet
Author: User

Original link here: http://blog.csdn.net/liulala16/article/details/8271673

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.

How the post and get requests differ:
1) Get link URL and parameters together, post URL and parameters are separated.
2) A GET request submits data up to 255 bytes, but the data submitted by the POST request is unlimited.
3) Get requests are not secure because URLs and parameters are together, but post requests are more secure because URLs and parameters are separate.
4) Get requests are frequently used to request data from the server, and post requests are often used to send data to the server. (e.g. uploading files)

Differences between synchronous and asynchronous requests:
Synchronous requests require the main thread to make data requests, and other operations will be blocked until the data is returned, causing the program to lag.
Asynchronous request is the main thread to find a sub-thread (younger brother) to make network data requests, so for other interface operation of the main thread can also do, there will be no program lag, so the network request we choose to use asynchronous request.

A POST request and a GET request are only two steps in the process of setting the request and setting the contents of the parameter, and the other operation is the same as the get operation.

How to set the request, (by default, a GET request)
[Request sethttpmethod:@ "POST"];
3: Set parameters, Package--Package Body Object
Convert parameters from NSString type to NSData type
NSString *parameters = @ "Method=album.channel.get&appkey=mykey&format=json&channel=t&pageno=1 &pagesize=10 ";
NSData *body = [parameters datausingencoding:nsutf8stringencoding];
[Request Sethttpbody:body];

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"];

//Second step, create a network request via 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)

          //third step, connecting to server

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

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

2. Synchronizing the 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"];// settings requestedby post, default to get

NSString *str = @ "type=focus-c";// Set parameters

NSData *data = [str datausingencoding:nsutf8stringencoding];

[Request Sethttpbody:data];

//Third step, connect to 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"];

//Second step, create request

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

//Third step, connect to 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"];

//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, connect to 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 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]);

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.