本文介绍了AFNetworking-发布请求 - 向正文添加简单文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用AFNetworking向发布请求添加简单字符串(无JSON或任何其他格式)?
我已经成功的最好是与'='结合。

How do i add a simple string (no JSON or any other format), to a post request using AFNetworking? The best i've already succeeded was concat with '='.

这个:

 NSURLRequest* request =[myServer multipartFormRequestWithMethod:@"POST" path:@"http://my.server.com" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSData *tmp_data = [NSString stringWithFormat:@"%@", @"my_string!"];
    [formData appendPartWithHeaders:nil body:tmp_data];
}];

提前致谢!

推荐答案

这很简单,这应该是答案:

As simple as is, This should be the answer:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.my.server.com"]];
[request setHTTPMethod:@"POST"];

//set headers
NSString *contentType = @"text/xml";
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request addValue:@"any-value" forHTTPHeaderField: @"User-Agent"];

//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[@"my_body_string!" dataUsingEncoding:NSUTF8StringEncoding]];

//post  
[request setHTTPBody:postBody];

从此处,使用http 请求执行您想要的操作(我用AFNetworking发送)。

From here, do what you want with the http request (i used with AFNetworking for sending).

干杯!

这篇关于AFNetworking-发布请求 - 向正文添加简单文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 21:32