iOS Development-get and Post

Source: Internet
Author: User

Directly on the code:

Example: (Drag two controls in storyboard TextField, one UIButton, connect (usernametext, password, 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/native ip/login.php"];

2.request

Nsmutablensurlrequest *request = [Nsmutablensurlrequest Requestwithurl:url];

The data title for the post request should be submitted with request

Request. HttpMethod = @ "POST";

Prepare a data question

NSString *bodystring = [NSString *stringwithformat:@ "username=%@&password=%@", Username,password];

Set up binary data

Request. Httpbody = [bodystring datausingencoding:nsutf8stringencoding];

  

3. Send the network connection to get the 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/native IP address/login.php?username =%@&password =%@", username, Password];

Percent-semicolon escape

URLString = [URLString stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];

Nsurl *url = [Nsurl urlwithstring:urlstring];

2.request

Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];

3. Send the network connection to get the 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//Native IP Address/login.php?username =%@&password =%@

HTTP://IP address/Log in script file? Parameter variable name = parameter value & (continuation parameter) variable name = value

Post

HTTP://IP address/Log in script

2.request

Get: There is nothing to handle, because the parameter is already specified in the URL

Post: Specify access method

HttpMethod = @ "POST";

  

Specifying binary data

Httpbody = binary data of the parameter string

3.NSURLConnection Asynchronous

Get is the same as post

In real-world web development, where the real change occurs, the request

iOS Development-get and 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.