使用AFNetworking在iOS上上传大文件 - 错误请求超时 [英] Uploading large files on iOS with AFNetworking - error Request Timeout

查看:1384
本文介绍了使用AFNetworking在iOS上上传大文件 - 错误请求超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临着下一个问题。
在我的项目中,我正在使用AFNetworking进行所有网络操作。其中之一是上传视频的服务器。
然后我试图上传大视频(大约100 Mb),我得到请求超时错误。


错误域= NSURLErrorDomain代码= -1001请求超时。
UserInfo = 0x15641b30

{NSErrorFailingURLStringKey = http://server.name/path
NSErrorFailingURLKey = http://server.name/path
NSLocalizedDescription = The请求超时。
NSUnderlyingError = 0x16f7a000请求超时。}

现在我正在使用AFNetworking v1 .3.3,我不能使用v2.0,因为需要iOS5支持。



当上传刚刚开始时,上传进度看起来很好(我通过UploadProgressBlock )。但是在几兆字节之后,上传开始变慢,然后停止。
SpeedTest给我上传5Mbps,下载5Mbps。

通过网络浏览器上传视频工作正常,所以我不认为这是服务器问题。

这里是我的代码:

  AFHTTPClient * client = [ AFHTTPClient clientWithBaseURL:
[NSURL URLWithString:@http://server.name/]];
NSString * appid = [[self class] sharedProvider] .ApplicationId;

ALAssetRepresentation * representaion =
[videoData.videoAsset defaultRepresentation];
NSURL * url =
[BRDataProvider getVideoAssetURLForTempFileWithAsset:
videoData.videoAsset];

AFHTTPRequestOperation *操作;
$ b $ if(url){
NSMutableURLRequest * request = [client multipartFormRequestWithMethod:@POST
path:@some / pathparameters:nil

constructBodyWithBlock:^(id< AFMultipartFormData> formData){

NSData * hdnADCID = [appid dataUsingEncoding:NSUTF8StringEncoding];
[formData appendPartWithFormData:hdnADCID name:@hdnADCID];
NSData * txtTitle =
[videoData.title dataUsingEncoding:NSUTF8StringEncoding];

[formData appendPartWithFormData:txtTitle name:@txtTitle];

NSData * txtDescription =
[videoData.description dataUsingEncoding:NSUTF8StringEncoding];
[formData appendPartWithFormData:txtDescription name:@txtDescription];

NSData * txtKeywords =
[videoData.tags dataUsingEncoding:NSUTF8StringEncoding];
[formData appendPartWithFormData:txtKeywords name:@txtKeywords];
[formData
appendPartWithFileURL:url name:representaion.filename error:nil];
}];
[request setTimeoutInterval:600];
operation = [fliqzClient HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation * operation,id responseObject){

[[NSFileManager defaultManager] removeItemAtURL:url error:nil];

NSString * assetID = [operation.responseString
stringByReplacingOccurrencesOfString:@& \r\withString:@];
assetID = [assetID stringByReplacingOccurrencesOfString:@id =
withString:@];
videoData.assetId = assetID;
[BRDataProvider registerVideoWithInfo:videoData completion:^(id result,
NSError * error){
block(result,error);
}];
}失败:^(AFHTTPRequestOperation *操作,NSError *错误){

NSLog(@error - %@,error);

block(nil,error);
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
}];
$ b $ [操作setUploadProgressBlock:^(NSUInteger bytesWritten,
long long totalBytesWritten,long long totalBytesExpectedToWrite){
NSLog(@bytesWritten - %d,totalBytesWritten - %lld,
totalBytesExpectedToWrite - %lld,bytesWritten,
totalBytesWritten,totalBytesExpectedToWrite);
}];
[client enqueueHTTPRequestOperation:operation];
}其他{
NSError *错误= [NSError errorWithDomain:kBRErDomainOwnDomain
code:0



userInfo:@ {NSLocalizedDescriptionKey:kPreprocessingErrorUploadVideoMessage }];
block(nil,error);
}

也许有人知道如何解决这个问题?
感谢您的帮助!

解决方案

我有类似的问题与AFNetworking:NSURLErrorDomain代码= -1001请求计时去。
当从外部服务器获取,并且设备位于连接到WAN的路由器的子网上时,请求可以很好地工作(192.168.1.0 subnet-1 - > WAN)。但是,如果连接到连接到与WAN相连的路由器的子网,请求将失败,并显示以上消息
(192.168.0.0 subnet-2 - > 192.168.1.0 subnet-1 - > WAN)。所有的浏览器操作都通过子网-2正常工作,AFNetworking似乎连接但是收到一个超时。我怀疑问题是与子网2路由器的配置。

I'm facing with the next problem. In my project, I'm using AFNetworking for all network operations. One of them is uploading video the the server. Then I'm trying to upload large video (about 100 Mb), I'm getting request timeout error.

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x15641b30
{NSErrorFailingURLStringKey=http://server.name/path, NSErrorFailingURLKey=http://server.name/path, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x16f7a000 "The request timed out."}

Now I'm using AFNetworking v1.3.3, and I can't use v2.0 because iOS5 support is a requirement.

When upload is just starting, upload progress looks fine (I see it through UploadProgressBlock). But after few megabytes, upload starts slowing, and later it stops. SpeedTest gives me 5Mbps for upload, and 5Mbps for download.

Upload video through web browser is working fine, so I don't think that is server problem.

Here is my code:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL: 
            [NSURL URLWithString:@"http://server.name/"]];
NSString *appid = [[self class] sharedProvider].ApplicationId;

ALAssetRepresentation *representaion =
             [videoData.videoAsset defaultRepresentation];
NSURL *url =
 [BRDataProvider getVideoAssetURLForTempFileWithAsset:
                                 videoData.videoAsset];

AFHTTPRequestOperation *operation;

if (url) {
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" 
    path:@"some/path" parameters:nil

constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

  NSData *hdnADCID = [appid dataUsingEncoding:NSUTF8StringEncoding];
     [formData appendPartWithFormData:hdnADCID name:@"hdnADCID"];
    NSData *txtTitle =
         [videoData.title dataUsingEncoding:NSUTF8StringEncoding];

 [formData appendPartWithFormData:txtTitle name:@"txtTitle"];

      NSData *txtDescription =
             [videoData.description dataUsingEncoding:NSUTF8StringEncoding];
    [formData appendPartWithFormData:txtDescription name:@"txtDescription"];

  NSData *txtKeywords =
       [videoData.tags dataUsingEncoding:NSUTF8StringEncoding];
    [formData appendPartWithFormData:txtKeywords name:@"txtKeywords"];
       [formData
     appendPartWithFileURL:url name:representaion.filename error:nil];
 }];
[request setTimeoutInterval:600];
operation = [fliqzClient HTTPRequestOperationWithRequest:request
   success:^(AFHTTPRequestOperation *operation, id responseObject) {

  [[NSFileManager defaultManager] removeItemAtURL:url error:nil];

   NSString *assetID = [operation.responseString
 stringByReplacingOccurrencesOfString:@"&\r\n" withString:@""];
       assetID = [assetID stringByReplacingOccurrencesOfString:@"id=
                           " withString:@""];
       videoData.assetId = assetID;
       [BRDataProvider registerVideoWithInfo:videoData completion:^(id result,
              NSError *error) {
          block(result,error);
           }];
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

               NSLog(@"error - %@", error);

                block(nil,error);
    [[NSFileManager defaultManager] removeItemAtURL:url error:nil];
                                                 }];

[operation setUploadProgressBlock:^(NSUInteger bytesWritten,
long long totalBytesWritten, long long totalBytesExpectedToWrite) {
   NSLog(@"bytesWritten - %d, totalBytesWritten - %lld,
     totalBytesExpectedToWrite - %lld", bytesWritten,
           totalBytesWritten, totalBytesExpectedToWrite);
}];
[client enqueueHTTPRequestOperation:operation];
} else {
NSError *error = [NSError errorWithDomain:kBRErrorDomainOwnDomain
                                     code:0



 userInfo:@{NSLocalizedDescriptionKey:kPreprocessingErrorUploadVideoMessage}];
    block(nil, error);
    }

Maybe someone knows the way how to fix it? Thanks for your help!

解决方案

I had similar issue with AFNetworking: NSURLErrorDomain Code=-1001 "The request timed out." When retrieving from an external server and the device is on a subnet to a router connected to the WAN, the request works perfectly (192.168.1.0 subnet-1 -> WAN). However, if connected to a subnet which is connected to the router connected to the WAN, the request fails with the above message (192.168.0.0 subnet-2 -> 192.168.1.0 subnet-1 -> WAN). All browser operations work fine through the subnet-2, AFNetworking appears to connect but receives a time-out. I suspect the problem is with a configuration of the subnet-2 router.

这篇关于使用AFNetworking在iOS上上传大文件 - 错误请求超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆