#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
网络:GET 登录和转码
最新推荐文章于 2023-05-23 15:04:17 发布