网络:GET 登录和转码

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, copy) NSString *username;//用户名
@property (nonatomic, copy) NSString *password;// 密码
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.username = @"张三";
    self.password = @"zhang";
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 拼接URLhttp://localhost/login.php?username=zhangsan&password=zhang
    // 如果URL中出现中文或者是空格或者其他的特殊字符串,就必须转百分号编码
    // 在真实开发中,项目名或者文件夹尽量不要使用中文
    NSString *URLString = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@",self.username,self.password];

    // 转码
    URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

//    NSLog(@"%@",URLString);

    // NSURL 在工作中,不止是php为后缀,还有可能是其他的,jsp,asp,.do,.action,不管什么后缀,在我们眼里都是一样的
    NSURL *url = [NSURL URLWithString:URLString];
    // NSURLRequest
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // NSURLConnection
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSLog(@"%@",response);
        id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        NSLog(@"%@",result);
    }];

}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值