GET请求
代码展示:
在storyBoard中每个请求关联一个Button
1 #pragma mark - get请求 2 - (IBAction)getRequest:(id)sender { 3 // 参数1:GET 请求的网址 4 // 参数2:拼接(body) 5 // 参数3:下载进度 downloadProgress 6 // 参数4:请求成功 responseObject:数据信息 7 // 参数5:请求失败 error错误信息 8 9 [self.session GET:@"http://api.yhouse.com/m/city/dynmiclist" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { 10 NSLog(@"下载进度"); 11 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 12 NSLog(@"请求成功%@", responseObject); 13 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 14 NSLog(@"请求失败"); 15 }]; 16 }
POST请求第一种
代码展示:
// 网络请求的头文件 #import <AFNetworking.h> @interface ViewController () // 通过AFHTTPSessionManager创建单例 @property (nonatomic,strong) AFHTTPSessionManager *session; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化session对象 self.session = [AFHTTPSessionManager manager]; // 设置请求接口回来时支持什么类型的数组 self.session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html", nil]; // Do any additional setup after loading the view, typically from a nib. }
实现请求操作:
1 #pragma mark - post请求 2 - (IBAction)postRequest:(id)sender { 3 /*{ 4 // body 5 do = "pri_memberlist"; 6 "member_id" = zpHr2dsRvQQxYJxo2; 7 "workspace_id" = ILfYpE4Dhs2gWcuQx; 8 9 // url 10 http://m.taskwedo.com/API/wedo1/wedo.php 11 }*/ 12 13 // 参数1:POST 请求的网址 14 // 参数2:拼接(body) 15 // 参数3:下载进度 downloadProgress 16 // 参数4:请求成功 responseObject:数据信息 17 // 参数5:请求失败 error错误信息 18 19 // 设置URL 20 NSString *url = @"http://m.taskwedo.com/API/wedo1/wedo.php"; 21 // 创建body 22 NSMutableDictionary *dic = [NSMutableDictionary dictionary]; 23 [dic setValue:@"pri_memberlist" forKey:@"do"]; 24 [dic setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"]; 25 [dic setValue:@"ILfYpE4Dhs2gWcuQx" forKey:@"workspace_id"]; 26 // 调用方法 27 [self.session POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) { 28 NSLog(@"上传进度"); 29 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 30 NSLog(@"post请求成功%@", responseObject); 31 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 32 NSLog(@"请求失败"); 33 }]; 34 }
POST请求第二种
代码展示:
1 #pragma mark - post请求 body中包含汉字 2 - (IBAction)anotherPostRequest:(id)sender { 3 4 // body体: 5 /* 6 address = ""; 7 comment = "U7c7bU6a21U5757U8ba1U5212U7528U5230U7b2cU4e09U90e8U5206U4e2dUff0cU5f85U63d0U95eeU3001U56deU7b54U79efU7d2fU5230U4e00U5b9aU6570U91cfU65f6Uff0cU4fbfU4e8eU5927U5bb6U7684U95eeU9898U7684U5febU901fU67e5U627eUff0cU6240U4ee5U63d0U95eeU90e8U5206U6682U65f6U4e0dU52a0U5165U8fd9U4e2a"; 8 do = "add_comment"; 9 kind = task; 10 "member_id" = zpHr2dsRvQQxYJxo2; 11 other = ""; 12 "task_id" = 55a47e79ec25e3641; 13 */ 14 // URL 15 // http://m.taskwedo.com/API/wedo1/wedo.php 16 // 设置URL 17 NSString *url = @"http://m.taskwedo.com/API/wedo1/wedo.php"; 18 // body中带有汉字的,需要进行转码 19 NSString *commonContent = @"类模块计划用到第三部分中,待提问、回答积累到一定数量时,便于大家的问题的快速查找,所以提问部分暂时不加入这个"; 20 commonContent = [commonContent stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; 21 // 创建body 22 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 23 [dict setValue:@"" forKey:@"address"]; 24 [dict setValue:@"commonContent" forKey:@"comment"]; 25 [dict setValue:@"add_comment" forKey:@"do"]; 26 [dict setValue:@"task" forKey:@"kind"]; 27 [dict setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"]; 28 [dict setValue:@"" forKey:@"other"]; 29 [dict setValue:@"55a47e79ec25e3641" forKey:@"task_id"]; 30 // 调用方法 31 [self.session POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) { 32 NSLog(@"上传进度"); 33 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 34 NSLog(@"请求成功%@", responseObject); 35 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 36 NSLog(@"请求失败"); 37 }]; 38 }