AFNetworking 3.0 multipartFormRequestWithMethod appendPartWithFileData丢失请求正文和标题

时间:2018-04-27 11:36:43

标签: ios image upload afnetworking multipart

使用AFNetwrking 1.0,图像的分段上传工作正常,我需要升级到3.0。但是,请求参数和正文消失了。它似乎在AFURLRequestSerialization.m中声明:

[self.request setHTTPBodyStream:self.bodyStream]

即使self.bodyStream中存在部件和正文。似乎没有setHTTPBodyStream返回错误。

这是代码:

AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

manager.securityPolicy.allowInvalidCertificates = YES; // not recommended for production

manager.securityPolicy.validatesDomainName = NO;

[requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"content-type"];

NSMutableURLRequest *request = [requestSerializer multipartFormRequestWithMethod:@"POST" URLString: ServerPath
                    parameters:sendDictionary constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

[formData appendPartWithFileData: imageData name:@"file" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
                         } error:nil];

[request setTimeoutInterval:20000];


NSURLSessionUploadTask *uploadTask;

NSLog(@"Request body %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]); NSLog(@"Request HTTP Headers: %@", request.allHTTPHeaderFields); NSLog(@"%@", [request HTTPBody]);

uploadTask = [manager

1 个答案:

答案 0 :(得分:0)

AFNetwrking 3.0显然必须使用Swift代码,因为它不再支持手动内存管理。不幸的是,你必须重写Objective C代码。