【问题标题】:AFNetworking 3.0 multipartFormRequestWithMethod appendPartWithFileData losing request body and headerAFNetworking 3.0 multipartFormRequestWithMethod appendPartWithFileData 丢失请求正文和标头
【发布时间】:2018-10-08 06:00:40
【问题描述】:

图像的分段上传在 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

【问题讨论】:

    标签: ios image upload afnetworking multipart


    【解决方案1】:

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

    【讨论】:

    • 好吧,我很惊讶,因为在目标 c 中有很多例子声称可以工作
    • 如果是苹果的话,事情很快就会崩溃,对不起
    • 好吧,我恢复到我的旧 AFNetorking 1.0 版本和相同的行为。尝试使用此论坛的另一种技术:appendPartWithFileURL and appendPartWithFormData,但它也不起作用。回到原来的版本,现在它可以工作了!谈论混乱。我将借此机会快速学习。谢谢你的回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    相关资源
    最近更新 更多