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

时间:2016-02-15 08:22:47

标签: ios objective-c http-post afnetworking-2

我一直在尝试通过将内容类型设置为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];

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:1)

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