Ios development-get and post, ios development-getpost

Source: Internet
Author: User

Ios development-get and post, ios development-getpost

Example: drag two controls textField and UIButton on the storyboard to connect them (userNameText, password, and login ))

@ Interface ViewController ()

@ Property (nonatomic, weak) IBOutlet UITextField * userNameText;

@ Property (nonatomic, weak) IBOutlet UITextField * passWord;

@ End

 

-(IBAction) login

{

[Self postLoginWithUserName: self. userNameText. text password: self. passWord. text];

}

Post Login

-(Void) postLoginWithUserName :( NSString *) userName password :( NSString *) password
{

// 1.url

NSURL * url = [NSURL URLWithString: @ "http: // local IP Address/login. php"];

// 2. request

NSMutableNSURLRequest * request = [NSMutableNSURLRequest requestWithURL: url];

// Submit the data question of the post request together with the request

Request. HTTPMethod = @ "POST ";

 

// Prepare data questions

NSString * bodyString = [NSString * stringWithFormat: @ "username = % @ & password = % @", username, password];

// Set binary data

Request. HTTPBody = [bodyString dataUsingEncoding: NSUTF8StringEncoding];

  

 

// 3. Send a network connection to obtain binary data

[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLReponse * response, NSData * data, NSError * connectionError)];

NSString * result = [NSString alloc] initWithData: data encoding: NSUTF8Encoding];

}

 

 

 

 

Get Login

-(Void) getLoginWithUserName :( NSString *) userName password :( NSString *) password

{

// 1.url

NSString * urlString = [NSString stringWithFormat: @ "http: // local IP Address/login. php? Username = % @ & password = % @ ", userName, password];

// Percent escape

UrlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

NSURL * url = [NSURL URLWithString: urlString];

// 2. request

NSURLRequest * request = [NSURLRequest requestWithURL: url];

// 3. Send a network connection to obtain binary data

[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLReponse * response, NSData * data, NSError * connectionError)];

NSString * result = [NSString alloc] initWithData: data encoding: NSUTF8Encoding];

}

 

Summary:

1.

Get:

Http: // local IP Address/login. php? Username = % @ & password = % @

Http: // ip address/login script file? (There are parameters later) parameter variable name = parameter value & (continue parameter) variable name = Value

Post

Http: // ip address/login script

2. request

Get: no processing is required because the parameter has been specified in the URL.

Post: Specifies the access method

HTTPMethod = @ "post ";

  

Specify binary data

HTTPBody = binary data of the parameter string

 

3. NSURLConnection asynchronous

Get is the same as post.

 

In actual network development, the request is where the change actually happens.

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.