Domain = NSURLErrorDomain Code = -1021"请求正文流耗尽"

时间:2013-04-17 19:07:55

标签: ios afnetworking

我收到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];


                                        }
                                    }

                                }];

2 个答案:

答案 0 :(得分:4)

AFNetworking FAQ

中所述
  

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

     

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

答案 1 :(得分:4)

我也遇到过这个问题,并且对throttleBandwithWithPacketSize方法没有任何好运。我认为这是一个身份验证挑战问题。

我最终做的是切换到AFNetworking 2.0中的URLSession连接方法,这似乎为我解决了这个问题。这是我最终使用的代码:

    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);
    }];