AFNetworking GET和POST请求

时间:2023-03-09 12:53:09
AFNetworking GET和POST请求

GET请求

代码展示:

在storyBoard中每个请求关联一个Button

 #pragma mark - get请求
- (IBAction)getRequest:(id)sender {
// 参数1:GET 请求的网址
// 参数2:拼接(body)
// 参数3:下载进度 downloadProgress
// 参数4:请求成功 responseObject:数据信息
// 参数5:请求失败 error错误信息 [self.session GET:@"http://api.yhouse.com/m/city/dynmiclist" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"下载进度");
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功%@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}

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.
}

实现请求操作:

 #pragma mark - post请求
- (IBAction)postRequest:(id)sender {
/*{
// body
do = "pri_memberlist";
"member_id" = zpHr2dsRvQQxYJxo2;
"workspace_id" = ILfYpE4Dhs2gWcuQx; // url
http://m.taskwedo.com/API/wedo1/wedo.php
}*/ // 参数1:POST 请求的网址
// 参数2:拼接(body)
// 参数3:下载进度 downloadProgress
// 参数4:请求成功 responseObject:数据信息
// 参数5:请求失败 error错误信息 // 设置URL
NSString *url = @"http://m.taskwedo.com/API/wedo1/wedo.php";
// 创建body
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:@"pri_memberlist" forKey:@"do"];
[dic setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"];
[dic setValue:@"ILfYpE4Dhs2gWcuQx" forKey:@"workspace_id"];
// 调用方法
[self.session POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"上传进度");
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"post请求成功%@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}

AFNetworking GET和POST请求

POST请求第二种

代码展示:

 #pragma mark - post请求 body中包含汉字
- (IBAction)anotherPostRequest:(id)sender { // body体:
/*
address = "";
comment = "\U7c7b\U6a21\U5757\U8ba1\U5212\U7528\U5230\U7b2c\U4e09\U90e8\U5206\U4e2d\Uff0c\U5f85\U63d0\U95ee\U3001\U56de\U7b54\U79ef\U7d2f\U5230\U4e00\U5b9a\U6570\U91cf\U65f6\Uff0c\U4fbf\U4e8e\U5927\U5bb6\U7684\U95ee\U9898\U7684\U5feb\U901f\U67e5\U627e\Uff0c\U6240\U4ee5\U63d0\U95ee\U90e8\U5206\U6682\U65f6\U4e0d\U52a0\U5165\U8fd9\U4e2a";
do = "add_comment";
kind = task;
"member_id" = zpHr2dsRvQQxYJxo2;
other = "";
"task_id" = 55a47e79ec25e3641;
*/
// URL
// http://m.taskwedo.com/API/wedo1/wedo.php
// 设置URL
NSString *url = @"http://m.taskwedo.com/API/wedo1/wedo.php";
// body中带有汉字的,需要进行转码
NSString *commonContent = @"类模块计划用到第三部分中,待提问、回答积累到一定数量时,便于大家的问题的快速查找,所以提问部分暂时不加入这个";
commonContent = [commonContent stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
// 创建body
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:@"" forKey:@"address"];
[dict setValue:@"commonContent" forKey:@"comment"];
[dict setValue:@"add_comment" forKey:@"do"];
[dict setValue:@"task" forKey:@"kind"];
[dict setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"];
[dict setValue:@"" forKey:@"other"];
[dict setValue:@"55a47e79ec25e3641" forKey:@"task_id"];
// 调用方法
[self.session POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"上传进度");
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功%@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}