Basic Network request, network request

Source: Internet
Author: User

Basic Network request, network request

1. Synchronize GET

// 1. initialize the URL into an OC String object

NSString *urlStr = [NSString stringWithFormat:@"%@?wewe=%@", @"111", @"222"];

// 2. If the URL contains Chinese characters, perform URLEncode NSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  // 3. Construct the network URL object, NSURL NSURL *url = [NSURL URLWithString:newUrlStr];  // 4. Create a network request NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];  // 5. Create a synchronization Link NSURLResponse *response = nil; NSError *error = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  Ii. asynchronous GET  The first four steps are basically the same.

// 1. initialize the URL into an OC String object

NSString *urlStr = [NSString stringWithFormat:@"%@?wewe=%@", @"111", @"222"];

// 2. If the URL contains Chinese characters, perform URLEncode NSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  // 3. Construct the network URL object, NSURL NSURL *url = [NSURL URLWithString:newUrlStr];  // 4. Create a network request NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];The difference is that here we create asynchronous request links // asynchronous links (Form 1, rarely used) [NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {       //your code      }]Generally, the first method is rarely used when an asynchronous connection is created, and the proxy method is often used. // Asynchronous connection (Form 2)[NSURLConnection connectionWithRequest: request delegate: self]; for NSURLConnectionDataDelegate, we often use the following protocol methods: // When the server receives the request - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { } // Triggered when the server receives the returned data. The returned data may be a resource fragment. You need to splice the resource fragment. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { } // Triggered when the server returns all data. The data is returned completely. - (void)connectionDidFinishLoading:(NSURLConnection *)connection { } // Triggered when the request data fails - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {      NSLog(@ "%@" , error); }  Iii. synchronous POST 

// 1. initialize the URL into an OC String object

NSString *urlStr = [NSString stringWithFormat:@"%@?wewe=%@", @"111", @"222"];

// 2. If the URL contains Chinese characters, perform URLEncode NSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  // 3. Construct the network URL object, NSURL NSURL *url = [NSURL URLWithString:newUrlStr];  // 4. Create a request NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  // 5. Create a parameter string object NSString *parmStr = @ "method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10" ;  // 6. Convert the string into an NSData object NSData *pramData = [parmStr dataUsingEncoding:NSUTF8StringEncoding];  // 7. Set the Request body[Request setHTTPBody: pramData]; // 8. Set the Request Method [request setHTTPMethod:@ "POST" ];       // 9. Create a synchronization Link      NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; Iv. asynchronous POST The first eight steps are basically the same. 

// 1. initialize the URL into an OC String object

NSString *urlStr = [NSString stringWithFormat:@"%@?wewe=%@", @"111", @"222"];

// 2. If the URL contains Chinese characters, perform URLEncode NSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  // 3. Construct the network URL object, NSURL NSURL *url = [NSURL URLWithString:newUrlStr];  // 4. Create a request NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  // 5. Create a parameter string object NSString *parmStr = @ "method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10" ;  // 6. Convert the string into an NSData object NSData *pramData = [parmStr dataUsingEncoding:NSUTF8StringEncoding];  // 7. Set the Request body [request setHTTPBody:pramData];  // 8. Set the Request Method [request setHTTPMethod:@ "POST" ];The difference is that here we create an asynchronous request link // 9. Create an asynchronous connection (Form 2) [NSURLConnection connectionWithRequest:request delegate:self];Execute the corresponding proxy method to complete the data request. Finally, let's analyze the differences between these network requests.

Difference between GET request and POST request:

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 will appear in the interface, which is not safe. POST requests are relatively secure.

3. 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.

Differences between synchronous and asynchronous:

Synchronization link: the main thread requests data. When other threads do not respond before the data request is completed, the program will be suspended.

Asynchronous link: A separate thread is opened to process network requests. The main thread is still interactive and the program runs smoothly.

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.