IOS-Http: GET: POST, ios-httpgetpost

Source: Internet
Author: User

IOS-Http: GET: POST, ios-httpgetpost
I. Overview

* The HTTP/1.1 protocol defines the eight request methods: OPTIONS, HEAD, GET, POST, PUT, DELETE, TRACE, and CONNECT.

* The GET method and POST method are the most frequently used network request methods.

* What is the difference between GET and POST in applications?

* The GET method sends a request to a specified resource. The message sent is displayed after the URL. The user information is insecure and the amount of information transmitted is limited. (The user name and password can be viewed in the request as follows)

 Http: // localhost: 8080/logandreg/logreg? Name = wyg & pwd = 1992

* If you only request data from the server without parameters, it is easier to use GET. (As shown below)

Http://www.baidu.com

* The amount of information transmitted by POST is large, and the transmitted information is hidden. The transfer information is secure. If you send data to the server, POST is recommended.

2. GET request network data (synchronous and asynchronous)

* As mentioned above, the GET method can send a request to a specified resource. For example, if we want to request an image to be displayed locally on the network, it is very convenient to use the GET method.

* GET requests are divided into synchronous requests and asynchronous requests. Generally, we use asynchronous requests for a good user experience.

* GET synchronization requests a network image (Code folding)

1 NSURL * url = [NSURL URLWithString: @ "http://img.ivsky.com/img/bizhi/slides/201508/18/september-012.jpg"]; 2 NSURLRequest * request = [NSURLRequest requestWithURL: url]; 3 NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; 4 _ iamgeview. image = [UIImage imageWithData: data];Synchronously retrieve network images

 * GET asynchronously requests a network image (iOS5.0) (Code folding ):

1 NSURL * url = [NSURL URLWithString: @ "http://img.ivsky.com/img/bizhi/slides/201508/18/september-012.jpg"]; 2 NSURLRequest * request = [NSURLRequest requestWithURL: url]; 3 NSOperationQueue * queue = [[NSOperationQueue alloc] init]; 4 [NSURLConnection sendAsynchronousRequest: request queue: queue completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {5 _ iamgeview. image = [UIImage imageWithData: data]; 6}];Asynchronous acquisition of network images

 * GET asynchronous request (iOS2.0) (Code folding)

1-(void) viewDidLoad 2 {3 [super viewDidLoad]; 4 NSURL * url = [NSURL URLWithString: @ "http://img.ivsky.com/img/bizhi/slides/201508/18/september-012.jpg"]; 5 NSURLRequest * request = [NSURLRequest requestWithURL: url]; 6 [NSURLConnection connectionWithRequest: request delegate: self]; 7} 8-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response; 9 {10 NSLog (@ "received response"); 11 _ buffer = [[NSMutableData alloc] init]; 12} 13-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data14 {15 NSLog (@ "received data"); 16 [_ buffer appendData: data]; 17} 18-(void) connectionDidFinishLoading :( NSURLConnection *) connection19 {20 NSLog (@ "request completed"); 21 _ iamgeview. image = [UIImage imageWithData: _ buffer]; 22} 23-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) erro24 {25 NSLog (@ "request error "); 26}Asynchronous request for network images

Iii. POST request network data (taking asynchronous as an example, synchronous data is similar to GET data)

* Transmission information is more secure than GET.

* The amount of information transmitted is larger than that of GET.

* The Code contains a detailed explanation. The Code is as follows:

 

-(IBAction) postRequest :( id) sender {// specify the request url NSURL * url = [NSURL URLWithString: @ "http: // localhost: 8080/logandreg/logreg"]; // create a request (variable request) NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; // specify the request method [request setHTTPMethod: @ "POST"]; // splice the parameter content NSString * body = @ "name = wyg & pwd = 1992"; // put the request data in the request body [request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]; // use post to initiate an asynchronous request [NSURLConnection connectionWithRequest: request delegate: self];}-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {// Method for response after receiving the response _ buffer = [[NSMutableData alloc] init];}-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {// Method for response after receiving data [_ buffer appendData: data];}-(void) connectionDidFinishLoading :( NSURLConnection *) connection {// method of response after data processing is complete NSString * str = [[NSString alloc] initWithData: _ buffer encoding: NSUTF8StringEncoding]; NSLog (@ "% @", str );} -(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {// Method for responding to a request error (such as network disconnection and timeout )}

 

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.