2020-07-30 ios post请求方式表单与Json
2020-08-05 本文已影响0人
缘來諟夢
最近遇到的坑,两个后台,一个表单,一个Json格式,之前一直没搞明白
附上源码供参考
// 1.创建一个请求管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//添加多的请求格式
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"application/x-www-form-urlencoded",@"text/plain",@"text/json", @"text/javascript",@"text/html",nil];
if (![URL containsString:@"##"]) {
//申明请求的数据是json类型,两种写法
manager.requestSerializer=[AFJSONRequestSerializer serializer];
// [manager.requestSerializer setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
}
else
{
//默认是表单
// [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
}
URL = [URL componentsSeparatedByString:@"##"][0];
NSLog(@"Url-->%@,\nParam-->%@",URL,parameters);
[manager POST:URL parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
// progress(uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[SVProgressHUD dismiss];
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD dismiss];
if (failure) {
failure(error);
}
}];