AFNetwork 以HTTPBODY 方式请求数据
2023-04-03 本文已影响0人
超人_22
AFNetwork 以HTTPBODY 方式请求数据
//afn 以httpbody 方式请求
-(void)afnetworkRequestWithHttpBodyMethod{
NSMutableDictionary * tempDict = [[NSMutableDictionary alloc] init];
tempDict[@"g"] = @"ApiElearning";
tempDict[@"m"] = @"Public";
tempDict[@"c"] = @"login";
tempDict[@"app_name"] = @"com.XXX.ssssGX_iphone";
tempDict[@"token"] = @"f1daoWwI1P7tw3Q2xwPVrMAcYBHlKp1+IhvYlNq3ChNkAhIBx8ARlu6uS1gQ";
tempDict[@"user_name"] = @"18080808080";
tempDict[@"password"] = @"IhvYlNq3ChNkAhIBx8ARlu6";
NSString *url = @"http://192.168.168.160/api.php?";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
request.HTTPMethod = @"POST";
NSData *data = [NSJSONSerialization dataWithJSONObject:tempDict options:NSJSONWritingPrettyPrinted error:nil];
NSString *tempStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
// 设置body 在这里将参数放入到body
request.HTTPBody = [tempStr dataUsingEncoding:NSUTF8StringEncoding];
AFURLSessionManager *manager =[[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",nil];
//设置成json请求方式
manager.responseSerializer = responseSerializer;
[[manager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
} downloadProgress:^(NSProgress * _Nonnull downloadProgress) {
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
LOG(@"%@", result);
}]resume];
}
//系统以httpbody 方式请求
+(void)UserLoginWithBackend {
NSMutableDictionary * tempDict = [[NSMutableDictionary alloc] init];
tempDict[@"g"] = @"ApiElearning";
tempDict[@"m"] = @"Public";
tempDict[@"c"] = @"login";
tempDict[@"app_name"] = @"com.XXX.ssssGX_iphone";
tempDict[@"token"] = @"f1daoWwI1P7tw3Q2xwPVrMAcYBHlKp1+IhvYlNq3ChNkAhIBx8ARlu6uS1gQ";
tempDict[@"user_name"] = @"18080808080";
tempDict[@"password"] = @"IhvYlNq3ChNkAhIBx8ARlu6";
// 1. 创建会话
NSURLSession *session = [NSURLSession sharedSession];
// 2. 创建任务
NSURL *url = [NSURL URLWithString:@"http://192.168.168.160/api.php?"];
NSData *data = [NSJSONSerialization dataWithJSONObject:tempDict options:NSJSONWritingPrettyPrinted error:nil];
// 创建请求对象里面包含请求体
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
request.HTTPMethod = @"POST";
NSString *tempStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
request.HTTPBody = [tempStr dataUsingEncoding:NSUTF8StringEncoding];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"%@", [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
// 打印解析后的json数据
LOG(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
}];
// 3. 执行任务
[task resume];
}
//系统以httpbody 方式请求1
+(void)userLogin2{
NSMutableDictionary * tempDict = [[NSMutableDictionary alloc] init];
tempDict[@"g"] = @"ApiElearning";
tempDict[@"m"] = @"Public";
tempDict[@"c"] = @"login";
tempDict[@"app_name"] = @"com.XXX.ssssGX_iphone";
tempDict[@"token"] = @"f1daoWwI1P7tw3Q2xwPVrMAcYBHlKp1+IhvYlNq3ChNkAhIBx8ARlu6uS1gQ";
tempDict[@"user_name"] = @"18080808080";
tempDict[@"password"] = @"IhvYlNq3ChNkAhIBx8ARlu6";
// 1.创建请求
NSURL *url = [NSURL URLWithString:@"http://192.168.168.160/api.php?"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 2.设置请求类型
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// 3.上传json数据
// NSData --> NSDictionary
// NSDictionary --> NSData
NSData *data = [NSJSONSerialization dataWithJSONObject:tempDict options:NSJSONWritingPrettyPrinted error:nil];
request.HTTPBody = data;
// 4.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
LOG(@"%@", result);
}];
}