; //肯定url NSURL *url = [NSURL URLWithString:urlstr]; //创建请求对象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //转为POST格局 request.HTTPMethod = @"POST"; //转码 request.HTTPBody = [@"username=(须要转的汉字)&pwd=520it&type=JSON" dataUsingEncoding:NSUTF8StringEncoding ]; NSURLResponse *response = nil; NSError *error = nil; //发送请求(同步请求) NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; //解析数据 NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); //发送请求(异步请求) [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { //解析数据 if (connectionError == nil) { NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); }else{ NSLog(@"%@",connectionError); } }]; } 七 NSURLSession简单应用
1 NSURLSession —->GET用法:
- - (void)get
- {
- //肯定请求路径
- NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
- //创建请求对象
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url ];
- //获取会话对象(是一个单例)
- NSURLSession *session = [NSURLSession sharedSession];
-
- //根据会话对象创建task
- /**
- * 参数一:请求对象
- *
- * 参数二:响应头response信息;响应体data信息;error缺点信息
- *
- *
推荐阅读
2018边缘数据中心关键之年!
沙龙晃荡 | 3月31日 京东、微博、华为拭魅战专家与你合营商量容器技巧实践!
所谓边沿数据中间,指的就是:处于>>>详细阅读
本文标题:iOS开发3年只用5分钟搞定面试官
地址:http://www.17bianji.com/lsqh/40950.html
1/2 1