Ios-http:get:post

Source: Internet
Author: User

I. Overview

* The http/1.1 protocol defines a total of 8 request methods: OPTIONS, HEAD, GET, POST, PUT, DELETE, TRACE, CONNECT.

* The Get method and post are the most Frequently used network request methods.

* What is the difference between get and post in the application?

* The Get method makes a request to the specified resource, and the message sent is followed by the URL, the user information is unsafe, and the amount of transmission is limited. (You can see the user name and password in the request, as shown below)

http://localhost:8080/logandreg/logreg?name=wyg&pwd=1992

* If you only request data from the server, without parameters, it is convenient to use get. (as shown below)

Http://www.baidu.com

* Post transmits a large amount of information, and the message is hidden, the transmission of information is more secure, if the data sent to the server, it is recommended to use post.

two. Get Request network data (synchronous, asynchronous)

* As mentioned above, the Get method can make a request to a specified resource, for example, we want to request a picture on the network to display locally, using the Get method is very convenient.

* Get requests are divided into synchronous and asynchronous requests, and in general we use asynchronous requests for a good user experience.

* Get sync Request a network picture (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];
Get network pictures synchronously

*get Asynchronously requests a network picture (iOS5.0) (code folding):

 1  nsurl *url = [nsurl urlwithstring:@ "  http://img.ivsky.com/img/bizhi/slides/201508/18/september-012.jpg  Span style= "color: #800000;" > " ";  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 }]; 
Get network Pictures asynchronously

*get Asynchronous Request (iOS2.0) (code folding)

1- (void) Viewdidload2 {3 [Super Viewdidload];4Nsurl *url = [Nsurl urlwithstring:@"http://img.ivsky.com/img/bizhi/slides/201508/18/september-012.jpg"];5Nsurlrequest *request =[Nsurlrequest Requestwithurl:url];6[Nsurlconnection Connectionwithrequest:requestDelegate: self];7 }8- (void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response;9 {TenNSLog (@"Receiving Response"); One_buffer =[[Nsmutabledata alloc]init]; A } -- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data - { theNSLog (@"receiving Data"); - [_buffer Appenddata:data]; - } -- (void) Connectiondidfinishloading: (Nsurlconnection *) Connection + { -NSLog (@"Request Complete"); +_iamgeview.image =[UIImage Imagewithdata:_buffer]; A } at- (void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) Erro - { -NSLog (@"Request Error"); -}
asynchronously request a network picture

Three. Post request network data (asynchronous for example, synchronous with get similar)

* Transmission information security is higher than get.

* The amount of transmitted information is larger than get.

* The code contains a detailed explanation, the code is as follows:

-(Ibaction) Postrequest: (ID) sender{//explicitly requested URL nsurl *url = [Nsurl urlwithstring:@] http://localhost:8080/logandreg/l    Ogreg "];    Create request (variable request) nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];    Specify the request method, [Request sethttpmethod:@ "POST"];    Stitching parameter content NSString *body = @ "name=wyg&pwd=1992";    Request data is placed in the requested body [request Sethttpbody:[body datausingencoding:nsutf8stringencoding]]; Initiate an asynchronous request using post [nsurlconnection connectionwithrequest:request delegate:self];} -(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{//method of responding after receiving response _buffe R = [[Nsmutabledata alloc]init];} -(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{//method of response after receiving data [_buffer appenddata:d ATA];} -(void) connectiondidfinishloading: (nsurlconnection *) connection{//method of response after data processing is complete nsstring *str = [[NSString alloc]i    Nitwithdata:_buffer encoding:nsutf8stringencoding]; NSLog (@ "%@", str);} -(void) connection:(nsurlconnection *) connection didfailwitherror: (Nserror *) error{//method of response after request error (such as network Off, timeout)} 

Ios-http:get:post

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.