NSURLSession之get请求模拟登录

get请求模拟登录

- (IBAction)login:(id)sender {

    

    // 0.请求地址

    NSString *URLString = [NSString stringWithFormat:@"http://localhost/php/login/login.php?username=%@&password=%@",self.usernameTextField.text,self.passwordTextField.text];

    

    // 0.1 GET请求的URL中有中文时,需要使用百分号转义(URLQueryAllowedCharacterSet : 允许URL里面的查询字符串进行百分号转义)

    URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    

    // 1.URL

    NSURL *URL = [NSURL URLWithString:URLString];

    

    // 2.获取单例session,发起和启动任务

    [[[NSURLSession sharedSessiondataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        

        // 3.错误处理

        if (error == nil) {

            

            // 4.反序列化

            NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

            // 5.判断登录是否成功

            if ([result[@"state"isEqualToString:@"OK"]) {

                NSLog(@"登录成功");

            } else {

                NSLog(@"登录失败");

            }

            

        } else {

            NSLog(@"%@",error);

        }

        

    }] resume];

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值