AFNetworking 将参数作为表单数据发送

AFNetworking send parameters as form data

我一直在尝试通过将内容类型设置为 application/x-www-form-urlencoded 到 HTTPHeaderField 来将参数作为表单数据发送,但是请求仅针对上述内容类型没有通过,我的代码适用于类型 application/json . 因此,我无法纠正实际问题,因此请求未通过。

下面是我的代码::

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setTimeoutInterval:60];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:requestAuthorization forHTTPHeaderField:@"Authorization"];
[manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:[[[UIDevice currentDevice] identifierForVendor] UUIDString] forHTTPHeaderField:@"device_id"];
[manager.requestSerializer setValue:[[UIDevice currentDevice] systemVersion] forHTTPHeaderField:@"device_os"];
[manager.requestSerializer setValue:[[UIDevice currentDevice] systemName] forHTTPHeaderField:@"device_name"];

[manager POST:url parameters:parameters success:success failure:failure];

如有任何帮助,我们将不胜感激。

谢谢

它在 Github 的 AFNetworking 页面上给出。 AFNetworking Library

您可以简单地使用以下示例来完成此操作:-

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

NSDictionary *parameters = @{@"key": @"xxx"};

[manager POST:@"http://test.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];