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);
-
}
-
}];