https://www.jianshu.com/p/d3d31e55fafa
https://blog.csdn.net/ximiaoweilai/article/details/102942632
post请求体封装如下:
- (void)postWithUrl:(NSString *)url body:(NSData *)body success:(void(^)(NSDictionary *response))success failure:(void(^)(NSError *error))failur
{
NSString *requestUrl = @“”;
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//如果你不需要将通过body传 那就参数放入parameters里面
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:requestUrl parameters:nil error:nil];
NSLog(@"requestURL:%@",requestUrl);
request.timeoutInterval= 10;
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// 设置body 在这里将参数放入到body
[request setHTTPBody:body];
AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
@"text/html",
@"text/json",
@"text/javascript",
@"text/plain",
nil];
manager.responseSerializer = responseSerializer;
[[manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse *response,id responseObject,NSError *error){
if(responseObject!=nil){
success(responseObject);
}
if (error) {
failure(error);
}
}]resume];
}
调用:
NSDictionary *dict = @{@"name":@"hello",@"sex":@"男"};
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSUTF8StringEncoding error:nil];
[self postWithUrl:@"" body:data showLoading:0 success:^(NSDictionary *response) {
//NSString *result = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:nil]; //解析
NSLog(@"%@",result);
} failure:^(NSError *error) {
}];
IOS AFN 通过body传递参数给服务器
2017-07-10 20:19 行走在砂砾中
- 此段代码适用于请求接口时传参为json格式,而非常见的dictionary。同时设置请求头的Content-Type: application/json;charset=UTF-8
- 先将AFN文件导入 或者pod 。
AFNetwoking的默认Content-Type是application/x-www-form-urlencodem。若服务器要求Content-Type为applicaiton/json,为了和服务器对应,就必须修改AFNetworking的Content-Type
===========刚在其他博主里看到更省事的代码,下面的这段代码不推荐。可以直接略过用第二段代码块===
-
NSDictionary *parameters= @{@"client_type":@"你的参数",@"你的参数":@"1"};
-
-
//设置参数 根据你们服务器的格式设置。我们的后台需要传的是json格式的
-
-
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
-
options:NSJSONWritingPrettyPrinte error:nil];
-
-
//afn请求
-
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:
-
[NSURLSessionConfiguration defaultSessionConfiguration]];
-
-
NSString * requestUrl = @"你的请求地址";
-
//如果你不需要在请求体里传参 那就参数放入parameters里面
-
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer]
-
requestWithMethod:@"POST" URLString:requestUrl parameters:nil error:nil];
-
-
// NSLog(@"requestURL:%@",requestUrl);
-
request.timeoutInterval= 10;
-
-
//这句话很重要,设置"Content-Type"类型 json类型跟后台大哥的一致
-
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
-
// 设置参数放入到body请求体里。后台大哥让参数放在请求体里,因为没写这句代码,我TM调试浪费了半天
-
[request setHTTPBody:jsonData];
-
-
AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
-
responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
-
@"text/html","text/json", @"text/javascript",@"text/plain", nil];
-
-
manager.responseSerializer = responseSerializer;
-
-
[[manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil
-
completionHandler:^(NSURLResponse *response,id responseObject,NSError *error){
-
-
if(responseObject!=nil){
-
-
NSString *result = [[NSString alloc] initWithData:responseObject
-
encoding:NSUTF8StringEncoding];
-
NSLog(@"%@",result);
-
-
}
-
-
}]resume];
第二种解决方法:推荐
-
-
NSDictionary *parameters= @{@"client_type":@"你的参数",@"你的参数":@"1"};
-
-
-
-
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
-
session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/plain", nil];
-
//post 发送json格式数据的时候加上这两句。
-
session.requestSerializer = [AFJSONRequestSerializer serializer];
-
session.responseSerializer = [AFJSONResponseSerializer serializer];
-
session.requestSerializer.timeoutInterval = 15;
-
// NSString * requestUrl = @"http://47.244.143.48:8080/auth/getVerificationCode";
-
NSString * requestUrl = @"http://192.168.1.10:8080/auth/getVerificationCode";
-
[session POST:requestUrl parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
-
-
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
-
if (responseObject) {
-
NSLog(@"post成功了%@",responseObject);
-
}
-
-
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
-
if (error) {
-
NSLog(@"post失败了%@",error);
-
}
-
}];