HTTP POST 请求提交数据有四种方式,具体可以参考post 提交数据四种方式。AFN 默认的方式是 multipart/form-data,而我们服务器不支持,需要以application/x-www-form-urlencoded 方式提交,在朋友的帮助下找到一种解决办法(以下代码基于 AFN 3.0及以上版本)。
NSDictionary* form = @{@"name":@"lisi",@"age":@(30), @"isMarryed":@(YES)};
NSMutableURLRequest* formRequest = [[AFHTTPRequestSerializerserializer]requestWithMethod:@"POST"URLString:routerAddressparameters:formerror:nil];
[formRequestsetValue:@"application/x-www-form-urlencoded; charset=utf-8"forHTTPHeaderField:@"Content-Type"];
AFHTTPSessionManager*manager = [AFHTTPSessionManagermanager];
AFJSONResponseSerializer* responseSerializer = [AFJSONResponseSerializerserializer];
[responseSerializersetAcceptableContentTypes:[NSSetsetWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",@"text/plain",nil]];
manager.responseSerializer= responseSerializer;
NSURLSessionDataTask*dataTask = [managerdataTaskWithRequest:formRequestuploadProgress:nildownloadProgress:nilcompletionHandler:^(NSURLResponse*_Nonnullresponse,id_NullableresponseObject,NSError*_Nullableerror) {
if(error) {
NSLog(@"Error: %@", error);
return;
}
NSLog(@"%@ %@", response, responseObject);
}];
[dataTaskresume];
请大家多多指点。
网友评论