我正面臨着下一個問題。 在我的項目中,我正在使用AFNetworking進行所有網絡操作。其中之一是將視頻上傳到服務器。 然後,我試圖上傳大型視頻(約100 Mb),我收到請求超時錯誤。使用AFNetworking在iOS上上傳大文件 - 錯誤請求超時
錯誤域= 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);
}
也許有人知道怎樣的方式來解決這個問題? 感謝您的幫助!
只需增加服務端的執行時間。 – sathiamoorthy
感謝您的建議。我們會嘗試,但也許有人知道我們可以在客戶端使用它做什麼?... – user1774316
@ user1774316假設您的服務器會尊重它,您可以使用'Keep-Alive'HTTP標頭請求更長的超時時間。 –