博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POST & GET
阅读量:4086 次
发布时间:2019-05-25

本文共 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/

你可能感兴趣的文章
十进制字符串转十六进制字符串
查看>>
属性字符串(富文本)的使用
查看>>
自定义大头针
查看>>
UIButton添加block点击事件
查看>>
利用runtime给类别添加属性
查看>>
本地推送
查看>>
FMDB的使用
查看>>
UIImage存为本地文件与UIImage转换为NSData
查看>>
[转]打印质数的各种算法
查看>>
[转]javascript with延伸的作用域是只读的吗?
查看>>
php的autoload与global
查看>>
IE不支持option的display:none属性
查看>>
[分享]mysql内置用于字符串型ip地址和整数型ip地址转换函数
查看>>
TableDnd(JQuery表格拖拽控件)应用进阶
查看>>
[转]开源中最好的Web开发的资源
查看>>
Https加密及攻防
查看>>
Java生成随机不重复推广码邀请码
查看>>
【JAVA数据结构】双向链表
查看>>
【JAVA数据结构】先进先出队列
查看>>
String类的intern方法随笔
查看>>