3

我收到 NSURLErrorDomain Code=-1021 “请求正文流已用尽”

NSLocalizedDescription=请求体流耗尽,NSUnderlyingError=0x2088c080 "请求体流耗尽"}

上传多张大尺寸图片时出现此错误我正在使用AFNetworking并尝试在线搜索修复,但没有成功

NSDictionary *clientUniqueId = [NSDictionary dictionaryWithObject:NSLocalizedString(uniqueDrId, nil) forKey:@"clientUniqueId"];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST"
                                                                 path:pendingUpload.urlPath
                                                           parameters:clientUniqueId
                                            constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
                                {
                                    [formData appendPartWithFormData:[pendingUpload dataRecordData] name:@"dr"];
                                    NSArray *attachments = pendingUpload.attachments;
                                    if (attachments != nil) {
                                        for (Attachment *attachment in attachments) {


                                            [formData appendPartWithFileData:attachment.data
                                                                        name:attachment.key
                                                                    fileName:attachment.filename
                                                                    mimeType:attachment.contentType];


                                        }
                                    }

                                }];
4

2 回答 2

4

I was experiencing this issue also and didn't have any luck with the throttleBandwithWithPacketSize method. I believe in my case it was an authentication challenge issue.

What I finally did was switch to the URLSession connection method in AFNetworking 2.0 and that seemed to solve it for me. Here is the code I ended up using:

    NSString *uploadAttachmentURL = @"https://mydomain.zendesk.com/api/v2/uploads.json?filename=screenshot.jpeg";

    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    _afHTTPSessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];

    // hack to allow 'text/plain' content-type to work
    NSMutableSet *contentTypes = [NSMutableSet setWithSet:_AFOpManager.responseSerializer.acceptableContentTypes];
    [contentTypes addObject:@"text/plain"];
    _afHTTPSessionManager.responseSerializer.acceptableContentTypes = contentTypes;

    [_afHTTPSessionManager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"[USERNAME]" password:@"[PASSWORD]"];



    [_afHTTPSessionManager POST:uploadAttachmentURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"screenshot" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        DDLogError(@"screenshot operation success!  %@", responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        DDLogError(@"Operation Error: %@", error);
    }];
于 2014-01-07T00:25:32.273 回答
4

AFNetworking 常见问题解答中所述:

为什么有些上传请求失败并显示错误“请求正文流已用尽”?这是什么意思,我该如何解决这个问题?

通过 3G 或 EDGE 连接上传时,请求可能会失败,并显示“请求主体流已耗尽”。使用-throttleBandwidthWithPacketSize:delay:您的多部分表单构造块,您可以根据推荐值(kAFUploadStream3GSuggestedPacketSizekAFUploadStream3GSuggestedDelay)设置最大数据包大小和延迟。这降低了输入流超出其分配带宽的风险。不幸的是,从 iOS 6 开始,没有明确的方法来区分 3G、EDGE 或 LTE 连接。因此,不建议您仅根据网络可达性来限制带宽。相反,您应该考虑在故障块中检查“请求主体流已耗尽”,然后使用限制带宽重试请求。

于 2013-05-26T18:04:24.587 回答