Synchronous requests, asynchronous requests, GET requests, POST requests for IOS, and iosget

Source: Internet
Author: User

Synchronous requests, asynchronous requests, GET requests, POST requests for IOS, and iosget

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

 

1. synchronous requests can request data from the Internet. Once a synchronous request is sent, the program stops user interaction until the server returns data,

2. asynchronous requests do not block the main thread, but create a new thread to operate. After the user sends an asynchronous request, the user can still operate the UI, and the program can continue to run.

3. For a GET request, write the parameters directly in the access path. The operation is simple, but it is easy to see by the outside world. The security is not high, and the address can be up to 255 bytes;

4. Put the parameters in the body in the POST request. POST request operations are relatively complex. Parameters and addresses must be separated. However, they are highly secure and placed in the body, which is not easy to capture.

 

Difference between POST and GET requests:
1) GET Concatenates the URL and parameters. In POST, the URL and parameters are separated.
2) the data submitted by the GET request can be up to 255 bytes, but there is no limit on the data submitted by the POST request.
3) The GET request is insecure because the URL and parameters are the same, but the POST request is safer because the URL and parameters are separated.
4) GET requests are often used to request data from the server, while POST requests are often used to send data to the server. (For example, uploading files)

 

Differences between synchronous and asynchronous requests:
Synchronous requests require the main thread to request data. Other operations will be blocked before the data is returned, resulting in program freezing.
Asynchronous requests are initiated by a subthread (younger brother) in the main thread to request network data. Therefore, other interface operations on the main thread can be performed without program freezing, therefore, asynchronous requests are used for network requests.

The post request and get request only have two more steps to set the Request Method and set the parameter content. Other operations are the same as GET operations.

// Set the Request Method (by default, it is a GET request)
[Request setHTTPMethod: @ "POST"];
// 3: set parameters, package -- package object
// Convert a parameter from the NSString type to the 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

 

// Step 1: Create a URL

NSURL * url = [NSURL URLWithString: @ "http://api.hudong.com/iphonexml.do? Type = focus-c "];

// Step 2: create a network request through URL

NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];

// The first parameter of the NSURLRequest initialization method: Request access path, second parameter: Cache Protocol, and third parameter: Network request timeout (seconds)

The cache Protocol is an enumeration type that includes:

NSURLRequestUseProtocolCachePolicy (Basic Policy)

NSURLRequestReloadIgnoringLocalCacheData (ignore local cache)

NSURLRequestReturnCacheDataElseLoad)

// Step 3: connect to the server

NSData * received = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

NSString * str = [[NSString alloc] initWithData: encoded ed encoding: NSUTF8StringEncoding];

 

2. synchronous POST request

// Step 1: Create a URL

NSURL * url = [NSURL URLWithString: @ "http://api.hudong.com/iphonexml.do"];

// Step 2: Create a request

NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];

[Request setHTTPMethod: @ "POST"]; // sets the request Method to POST. The default value is GET.

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

NSData * data = [str dataUsingEncoding: NSUTF8StringEncoding];

[Request setHTTPBody: data];

 

// Step 3: connect to the server

NSData * received = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

NSString * str1 = [[NSString alloc] initWithData: encoded ed encoding: NSUTF8StringEncoding];

NSLog (@ "% @", str1 );

3. asynchronous GET request

// Step 1: Create a url

NSURL * url = [NSURL URLWithString: @ "http://api.hudong.com/iphonexml.do? Type = focus-c "];

// Step 2: Create a request

NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];

// Step 3: connect to the server

NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate: self];


4. asynchronous POST request

// Step 1: Create a url

NSURL * url = [NSURL URLWithString: @ "http://api.hudong.com/iphonexml.do"];

// Step 2: Create a 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];

// Step 3: connect to the server

NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate: self];



5. Proxy Method for asynchronous requests

 

// Call this method when receiving a response from the server

-(Void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response

{

NSHTTPURLResponse * res = (NSHTTPURLResponse *) response;

NSLog (@ "% @", [res allHeaderFields]);

Self. receiveData = [NSMutableData data];


}

// Called when receiving data transmitted by the server. This method is executed several times based on the data size.

-(Void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data

{

[Self. receiveData appendData: data];

}

// Call this method after data transmission.

-(Void) connectionDidFinishLoading :( NSURLConnection *) connection

{

NSString * receiveStr = [[NSString alloc] initWithData: self. receiveData encoding: NSUTF8StringEncoding];

NSLog (@ "% @", receiveStr );

}

// Any errors (Network disconnection, connection timeout, etc.) occur during the network request.

-(Void) connection :( NSURLConnection *) connection

DidFailWithError :( NSError *) error

{

NSLog (@ "% @", [error localizedDescription]);

 

 

 

 

 

 

 

 

 

 

 

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.