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.