无法下载文件 收到此错误“客户端在收到整个响应之前关闭了连接” 我试图下载的文件只有266KB
for (NSString *downloadURL in arr) {
// NSString *downloadURL = [d objectForKey:@"url"];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"URL for downloading : %@",downloadURL);
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *downloadedZipPath = [documentsDirectory stringByAppendingPathComponent:TEMP_DICTIONARY];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:downloadURL]];
// [request addValue:@"bytes=x-" forHTTPHeaderField:@"Range"];
// [request setTimeoutInterval:20];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:downloadedZipPath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
});
试图增加api请求时间,但仍无法正常工作
答案 0 :(得分:0)
我有这样的错误。但我认为这不是AF故障。当您请求的内容类型与其原始内容类型不同时,此错误将会出现。
例如:
当您请求mp4文件时,服务器应将content-type设置为video / mpeg4,或者您可以将请求类型从GET更改为POST。
答案 1 :(得分:0)
尝试将其设置为更长时间
[request setTimeoutInterval:20];