1.初始化字典并设置key-value。
(注意:字典参数不需要转成Json)
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[dic setObject:@"balabala" forKey:@"Name"];
[dic setObject:@"123456" forKey:@"Id"];
...
2.设置AFHTTPSessionManager:
//这里配置请求类型为Json
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
3.正常发起Post请求:
[manager POST:url parameters: params progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
MYLog(@"success");
NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
if ([responses statusCode] == 200) {
//success
do u method...
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
MYLog(@"failure");
}];
网友评论