AFNetworking 请求参数中含有二进制图片信息

来源:互联网 发布:淘宝卖家后台官网 编辑:程序博客网 时间:2024/06/05 00:54

    AFHTTPSessionManager *manager = [AFHTTPSessionManagermanager];


    manager.requestSerializer = [AFJSONRequestSerializerserializer];

    manager.responseSerializer = [AFHTTPResponseSerializerserializer];

    manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",nil];

    [manager.requestSerializersetValue:@"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)}



阅读全文
0 0
原创粉丝点击