AFHTTPSessionManager将主体添加到POST
我也需要向我的服务器发送邮件请求。
使用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) { }];
我如何将正文添加到“AFMultipartFormData”?
感谢advace
从AFNetworking 主页取得 ,要创build一个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
请求(例如一个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); }];
试试这个方法。
我正在转换我的图像数据格式是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); }];
- AFHTTPSessionManager子类swift
- 调用ExtensionDelegate为复杂度创build/刷新数据
- 使用AFNetworking下载文件时的内存警告
- HKWorkoutSession:当watchOS中的屏幕closures时,没有获得心率
- iOS – 增加AFHTTPRequestOperationManager超时
- 在WatchOS 2.0中使用核心数据
- 离子2,在iOS上使用Angular 2pipe道中断 – “找不到variables:Intl”
- 如何使用MAC在真实设备上构build和部署ios api文件
- 在运行AFHTTPSessionManager的AFNetworking 2中设置对并发任务的限制