AFHTTPSessionManager *manager = [AFHTTPSessionManagermanager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
[manager.requestSerializer setValue:@"multipart/form-data;" forHTTPHeaderField:@"Content-Type"];
[manager POST:@"网址"parameters:@{图片以外的键值对参数}constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
1.
// [formData appendPartWithFormData:UIImageJPEGRepresentation([UIImage imageNamed:@"123.jpg"], 1.0) name:@"picture"];
2.
[formData appendPartWithFileData:UIImageJPEGRepresentation([UIImageimageNamed:@"123.jpg"],1.0) name:@"picture"fileName:@"123.jpg"mimeType:@"image/jpeg"];
} progress:nilsuccess:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
NSLog(@"111---%@",responseObject);
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:responseObjectoptions:NSJSONReadingMutableContainerserror:nil];
NSLog(@"222---%@", dic);
} failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
NSLog(@"error == %@", error);
}];
第一种方式会报错
报错信息为:
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x608000424e00> { URL: https://ocr.shitijun.com/ } { status code: 400, headers {
Connection = "keep-alive";
"Content-Type" = "text/html; charset=utf-8";
Date = "Wed, 19 Jul 2017 08:48:30 GMT";
Server = "nginx/1.4.6 (Ubuntu)";
"Transfer-Encoding" = Identity;
"X-Frame-Options" = SAMEORIGIN;
} }, NSErrorFailingURLKey=https://ocr.shitijun.com/, com.alamofire.serialization.response.error.data=<>, NSLocalizedDescription=Request failed: bad request (400)}