gpt4 book ai didi

ios - AFNetworking 下载重定向

转载 作者:行者123 更新时间:2023-11-29 03:29:05 27 4
gpt4 key购买 nike

我尝试使用 AFNetworking 下载文件,下载内容被重定向到 Amazon Web Services。我可以看到重定向,但它似乎永远不会下载,所以我想知道我做错了什么!这是我的代码:

NSString *urlString = [[NSString alloc]initWithFormat:@"%@?%@=%@", update.downloadUrl.absoluteString, @"auth_token", database.userAuthToken];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
NSLog(@"Response: %@", redirectResponse.debugDescription);
NSLog(@"Request: %@", request.debugDescription);

if(redirectResponse == nil) return request;

NSMutableURLRequest *urlRequest = [httpClient requestWithMethod:@"GET" path:request.URL.absoluteString parameters:nil];
return urlRequest;
}];

[operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:[item.downloadUrl absoluteString] append:false]];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
[_currentItem setUpdateProgress:totalBytesRead / (float)totalBytesExpectedToRead];
NSLog(@"BytesRead: %i, TotalBytesRead: %lld, BytesToRead: %lld", bytesRead, totalBytesRead, totalBytesExpectedToRead);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[self networkQueueComplete:operation withResponse:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self networkQueueFailed:operation withError:error];
}];

[networkQueue addOperation:operation];

在 setRedirectResponse block 中,我可以看到正确的重定向 url,但我的 setDownloadProgress block 永远不会被调用。

最佳答案

我在我的服务器上添加了一个重定向页面,然后运行您的代码。我发现如果直接在 setRedirectResponseBlock: 中返回请求,重定向下载工作正常,setDownloadProgress block 被调用。

[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
NSLog(@"Response: %@", redirectResponse.debugDescription);
NSLog(@"Request: %@", request.debugDescription);
return request;
}];

更新:

我的测试重定向页面:http://joiningsstest.byethost11.com/redirect.php您可以在 http/get 中使用“url”参数传递您的重定向 url。

我不确定您的 s3 url 是否需要访问授权。在我的测试代码中,我使用“http://still.s3.amazonaws.com/Material/Chimney/images/1.jpg”进行重定向,这是一个公共(public) s3 url。

重定向页面的代码:

<?php
if($_GET['url'])
{
header('HTTP/1.1 302 Moved Permanently');
header('Location:'.$_GET['url']);
}
?>

我的测试代码:

NSString * redirectURL = @"http://still.s3.amazonaws.com/Material/Chimney/images/1.jpg";
NSString * testURL = [NSString stringWithFormat:@"http://joiningsstest.byethost11.com/redirect.php?url=%@",redirectURL];
NSURLRequest * originalRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:testURL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:originalRequest];
[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
NSLog(@"Response: %@", redirectResponse.debugDescription);
NSLog(@"Request: %@", request.debugDescription);
return request;
}];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"1.jpg"];
[operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:path append:false]];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"BytesRead: %i, TotalBytesRead: %lld, BytesToRead: %lld", bytesRead, totalBytesRead, totalBytesExpectedToRead);
}];
[operation start];

关于ios - AFNetworking 下载重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20056894/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com