本文共 3516 字,大约阅读时间需要 11 分钟。
#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}/// POST请求模拟登录的主方法 : 正确的姿势- (IBAction)login:(id)sender { NSString *URLString = @"http://localhost/php/login/login.php"; // 1.URL NSURL *URL = [NSURL URLWithString:URLString]; // 2.request NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; // 设置请求方法为@"POST" request.HTTPMethod = @"POST"; // 设置请求体 request.HTTPBody = [self getHTTPBody]; // 3.session发起和启动任务 [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 4.处理响应 if (error == nil && data != nil) { // 反序列化 NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]; // 判断是否登录成功 if ([result[@"userId"] intValue] == 1) { NSLog(@"登录成功"); } else { NSLog(@"登录失败"); } } else { NSLog(@"%@",error); } }] resume];}- (NSData *)getHTTPBody{ // 检测到请求体 : username=%E5%BC%A0%E4%B8%89&password=zhang NSString *HTTPBody = [NSString stringWithFormat:@"username=%@&password=%@",self.usernameTextField.text,self.passwordTextField.text]; // 快速的把字符串转成二进制 NSData *data = [HTTPBody dataUsingEncoding:NSUTF8StringEncoding]; return data;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}/// 发送GET请求去登录- (IBAction)login:(id)sender { NSString *name = self.userNameTextField.text; NSString *password = self.passwordTextField.text; NSString *URLString = [NSString stringWithFormat:@"http://localhost/php/login/login.php?username=%@&password=%@",name,password]; // 对查询字符串进行转义 : 因为GET请求时,URL里面不能出现中文,空格等特殊字符.否则,URL为nil URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; // http://localhost/php/login/login.php?username=%E5%BC%A0%E4%B8%89&password=zhang NSLog(@"%@",URLString); // 1.URL NSURL *URL = [NSURL URLWithString:URLString]; // 2.session发起和启动任务 [[[NSURLSession sharedSession] dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 3.处理响应 if (error == nil && data != nil) { // 反序列化 NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]; // 判断是否登录成功 if ([result[@"userId"] intValue] == 1) { NSLog(@"登录成功"); } else { NSLog(@"登录失败"); } } else { NSLog(@"%@",error); } }] resume];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
转载地址:http://vfkii.baihongyu.com/