1、#import "AFHTTPSessionManager.h"
2、 //网络请求管理器
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
3、 //设置请求内容的类型
//重中之重:添加header前先初始化AFJSONRequestSerializer,否则无法添加
sessionManager.requestSerializer = [AFJSONRequestSerializer new];
[sessionManager.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"content-type"];
[sessionManager.requestSerializer setValue:@"text/xml,application/json" forHTTPHeaderField:@"Accept"];
[sessionManager.requestSerializer setValue:@"zh-CN,zh;q=0.8" forHTTPHeaderField:@"Accept-Language"];
[sessionManager.requestSerializer setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
[sessionManager.requestSerializer setValue:@"WLJY-ANDROID" forHTTPHeaderField:@"client-type"];
[sessionManager.requestSerializer setValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];
4、
[sessionManager POST:requestUrlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//发送内容 (NSData到后台即是file类型)下面举例上传一次多张图片,array即图片数组
for(int i =0; i< array.count ; i++){
NSData*imageData = (图片转NSData 自己查 取单张图片array[i])
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat=@"yyyyMMddHHmmss";
NSString*str = [formatter stringFromDate:[NSDatedate]];
NSString*fileName = [NSString stringWithFormat:@"%@.jpg", str];
[formData appendPartWithFileData:imageData name:@"img" fileName:fileName mimeType:@"multipart/form-data"];
}
}progress:^(NSProgress*_NonnulluploadProgress) {
nil;
}success:^(NSURLSessionDataTask*_Nonnulltask,id _NullableresponseObject) {
NSLog(@"success");
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
NSLog(@"default");
}];
}