我正面临下一个问题。在我的项目中,我将 AFNetworking 用于所有网络操作。其中之一是将视频上传到服务器。然后我尝试上传大视频(大约 100 Mb),我收到请求超时错误。
错误域=NSURLErrorDomain 代码=-1001 “请求超时。” UserInfo=0x15641b30
{NSErrorFailingURLStringKey= http://server.name/path , NSErrorFailingURLKey= http://server.name/path , NSLocalizedDescription=请求超时。, 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 *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);
}
也许有人知道如何解决它?谢谢你的帮助!