AFNetworking POST非JSON字符串

时间:2015-10-02 09:41:34

标签: ios objective-c json afnetworking

我正在尝试使用AFNetworking库创建POST请求。

[self.manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id response)
{
    // CODE
}

failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
    // CODE
}];

有没有办法使用AFNetworking将简单字符串(不是JSON字符串)作为请求正文参数发布?

1 个答案:

答案 0 :(得分:0)

是的,你就是这样做的:

NSString *someString = @"SomeString";
NSData* stringData = [someString dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"someUrlString"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy  timeoutInterval:20];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: stringData  ];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
   //Success Block
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure Block
}];
[op start];