我也需要向我的服务器发帖子。
使用 AFHTTPRequestOperation 非常简单,只需使用:
[request setHTTPBody: [requestBody dataUsingEncoding:NSUTF8StringEncoding]];
但是,我无法找到使用 AFHTTPSessionManager 的相同方法的任何示例。
使用方法:
[self POST:@"extraLink" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
} success:^(NSURLSessionDataTask *task, id responseObject) {
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
我如何将身体添加到&#34; AFMultipartFormData&#34;?
先谢谢
答案 0 :(得分:12)
从AFNetworking home page获取,要创建multipart/form-data
请求,请致电appendPartWithFileURL
:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
但不推荐使用AFHTTPRequestOperationManager
。因此,请使用AFHTTPSessionManager
,POST
与constructingBodyWithBlock
的语法非常相似:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} progress:nil success:^(NSURLSessionDataTask *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
或者,如果您要发布foo=bar&key=value&...
形式的请求(即application/x-www-form-urlencoded
请求),您可以执行以下操作:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *parameters = @{@"foo": @"bar", @"key": @"value"};
[manager POST:@"http://example.com/resources.json" parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Error: %@", error);
}];
答案 1 :(得分:4)
尝试另一种方式。
我正在以imageData
的数据格式转换我的图片。
NSData *imageData=nil;
imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"yourimageName"],0.2);
NSMutableDictionary *dict=[NSMutableDictionary new];
[dict setObject:@"user1" forKey:@"param_name1"];
[dict setObject:@"User2" forKey:@"param_name2"];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
manager.responseSerializer = [AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager POST:@"API NAME" parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
if(imageData)
{
[formData appendPartWithFileData:imageData name:@"param_name" fileName:@"filename.jpg" mimeType:@"image/jpeg"];
}
} progress:^(NSProgress * _Nonnull uploadProgress)
{
NSLog(@"%@",uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];