AFNetworking下载重定向

时间:2013-11-18 20:11:09

标签: ios afnetworking

我正在尝试使用AFNetworking下载文件,下载将重定向到Amazons 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块中,我可以看到正确的重定向网址,但我的setDownloadProgress块永远不会被调用。

1 个答案:

答案 0 :(得分:1)

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

[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”参数传递重定向网址。

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

重定向页面的代码:

<?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];