Ios network programming (Entry level)-basic knowledge, ios Network Programming

Source: Internet
Author: User

Ios network programming (Entry level)-basic knowledge, ios Network Programming

In the process of learning ios, I stayed in the UI control for a long time. Now I am gradually getting in touch with it !!!!!! In this process, I learned some knowledge about network programming and shared it here:

I don't need to talk about the importance of network requests !!! For mobile clients, the importance of the network is self-evident. Common network requests include GET and POST. Let's take a look at the implementation methods of the two network requests today.

Understanding of the First Part

I. Two types of network requests-similarities and differences between POST requests and GET requests (Here we only discuss asynchronous links)

The GET syntax is to GET the resources on the specified URL, add the data to the URL indicated by the action in the form of Variable = Value, and use "? "Connection. Use" & "connection between variables.

The POST syntax is to specify the "APPEND/Add" resource of the resource, put the data on the data body, and pass the data to the URL pointed to by the action according to the corresponding variables and values.

1. The GET request interface will include the parameter part. The parameter will be part of the URL? . The post request separates the server address from the parameter. The request interface only contains the server address, and the parameter is used as part of the request and submitted to the backend server.

2. The GET Request Parameters appear in the interface, which is not secure. the post request is relatively secure and all data is invisible to users.

3. The size of data transmitted by GET is small, which is mainly limited by the URL length. POST can transmit a large amount of data, and only POST can be used to upload files.

4. Although both GET requests and POST requests can be used to request and submit data, generally GET requests are mostly used to request data from the background, and POST requests are mostly used to submit data to the background.

Ii. Network request steps
1. Confirm the address NSURL
2. Create a request for NSURLRequest
3. Establish and start NSURLConnection
Waiting for network processing ~~~~
4. process network requests through proxy
Compliance Agreement: NSURLConnectionDataDelegate


Iii. Network Proxy Methods
1. The server receives the response from the server. The server needs to transmit data and the client prepares for receiving the response.
2. receive data transmitted by the server, which may be executed multiple times
3. The data is received and processed later.
4. Server Request failure due to many reasons (network environment, etc)
5. send data to the server. This method is only applicable to POST, especially when uploading files.


Iv. GET Method
In NSURL, specify a parameter (if any, you can also leave it unspecified), for example: http://www.baidu.com


V. POST Method
Define a variable URLMutableRequest
1) Duration
[Request setTimeoutInterval: 2.0f];
2) Request Method (GET by default)
[Request setHTTPMethod: @ "POST"];
3) data body
NSData * body = [string dataUsingEncoding: NSUTF8StringEncoding];
[Request setHTTPBody: body];
 
6. Synchronization Method-a network request must be completed before subsequent execution, such as online banking Login
You can see that the method parameter contains the _ autoreleasing text, and add "&" before the Defined Object Name.
NSURLResponse * response = nil;
NSError * error = nil;
// The synchronization operation is not completed and subsequent code will not be executed
NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: & error];
1> received data, indicating that the operation is normal
2> no data is received, but the error is nil, indicating that NULL data is received.
Generally, the server does not respond to this request.
3> error is not empty, indicating a request error

VII. Asynchronous Method
[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * reponse, NSData * data, NSError * error ){
 
// Processing after the request is completed
}];
 
Asynchronous method does not need to wait until the network request ends

 

Part 2 demo

 

1 // 1. Confirm the address NSURL 2 NSString * urlString = [NSString stringWithFormat: @ "http://XXXXXXX.php? XXXX =%@ & XXXX =%@ ", XXX, XXX; 3 urlString = [urlString encoding: NSUTF8StringEncoding]; 4 NSURL * url = [NSURL URLWithString: urlString]; 5 // 2. create a request NSURLRequest 6 NSURLRequest * request = [NSURLRequest requestWithURL: url]; 7 // 3. establish and start NSURLConnection 8 NSURLConnection * conn = [NSURLConnection connectionWithRequest: request delegate: self]; 9 // start the connection, asynchronous connection request 10 [conn start]; 11 // server notification preparation and preparation of transit data 12 self. serverData = [NSMutableData data];GET method 1 // 1. confirm the address NSURL 2 NSString * urlString = [NSString stringWithFormat: @ "http://XXXXX.php"]; 3 NSURL * url = [NSURL URLWithString: urlString]; 4 // 2. create a request NSURLRequest (POST) 5 NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; 6 // 1) request Method 7 [request setHTTPMethod: @ "POST"]; 8 // 2) Data body 9 // because dataUsingEncoding has implemented transcoding 10 NSString * bodyStr = [NSString stringWithFormat: @ "username = % @ & password = % @", userName, pwd]; 11 NSData * body = [bodyStr dataUsingEncoding: NSUTF8StringEncoding]; 12 [request setHTTPBody: body]; 13 // 3. establish and start NSURLConnection14 NSURLConnection * conn = [NSURLConnection connectionWithRequest: request delegate: self]; 15 // start the connection, asynchronous connection request 16 [conn start]; 17 // server notification preparation and preparation of transit data 18 self. serverData = [NSMutableData data];POST method 1-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {} 2-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {} 3-(void) connectionDidFinishLoading :( NSURLConnection *) connection {} 4-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {} 5-(void) connection :( NSURLConnection *) connection didSendBodyData :( NSInteger) bytesWritten totalBytesWritten :( NSInteger) totalBytesWritten totalBytesExpectedToWrite :( NSInteger) totalBytesExpectedToWrite6 {}Network Proxy Method

 

I hope you can give me some advice !!!!!!

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.