Ios 无法接收GET请求的上次重定向的URL

Ios 无法接收GET请求的上次重定向的URL,ios,objective-c,web-services,redirect,Ios,Objective C,Web Services,Redirect,我想做什么: 当我向URL发出GET请求时,响应是302 HTTP响应,这意味着它是一个重定向。我知道在我最初的GET请求之后有多个重定向(3到4)。我想得到多个重定向发生后的最后一个URL。我只能收到第一个重定向URL 我尝试的是以下方面的答案: 我的代码剪取自以下答案: AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSString *URLString = @"

我想做什么:

当我向URL发出
GET
请求时,响应是
302 HTTP响应
,这意味着它是一个重定向。我知道在我最初的
GET
请求之后有多个重定向(3到4)。我想得到多个重定向发生后的最后一个URL。我只能收到第一个重定向URL

我尝试的是以下方面的答案:

我的代码剪取自以下答案:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *URLString = @"URL";
NSDictionary *parameters = @{@"k1": @"v1", @"k2": @"v2"};
NSMutableURLRequest *request_orig = [manager.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:manager.baseURL] absoluteString] parameters:parameters error:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request_orig];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
     //This is the first Redirect URL that I receive 
     NSLog(@"New redirect URL: %@",[[[operation response] URL] absoluteString]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
     NSLog(@"Failure: err: %@", error);
}];


[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {

    if (redirectResponse) {
        NSMutableURLRequest *r = [request_orig mutableCopy]; // original request
        [r setURL: [request URL]];
        return r;
    } else {
        NSLog(@"redirecting to : %@", [request URL]);
        return request;
    }        
}];

[manager.operationQueue addOperation:operation];

我能够收到第一个重定向的URL,但之后没有第二个或第三个重定向。我应该怎么做才能让重定向继续并只接收最后一次重定向的URL。我是iOS开发的新手。我将感谢您的帮助。谢谢

如果您只想执行所有重定向,则在数据开始通过连接时,继续返回提供的请求并中止就足够了

我不久前为OSX写过这篇文章,但它应该可以在iOS(w/ARC)上运行,而不需要修改:

- (NSURL *)resolvedURLRedirectionsForURL:(NSURL *)aURL {
    NSLog(@"Started w/ initial URL: '%@'", aURL);
    NSURL           *originalURL    = aURL;
    NSURL __block   *resolvedURL    = nil;

    if (aURL) {
        dispatch_semaphore_t __communicationLock = dispatch_semaphore_create(0);

        NSURLRequest *resolveRequest = [NSURLRequest requestWithURL:originalURL
                                                        cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                    timeoutInterval:5.  ];
        AFHTTPRequestOperation *request = [[AFHTTPRequestOperation alloc] initWithRequest:resolveRequest];

        [request setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {

            NSLog(@"..request encountered redirection to: '%@'",request.URL);

            if (redirectResponse)
                resolvedURL = [request.URL copy];

            return request;
        }];

        AFHTTPRequestOperation __weak *weakRequest = request;
        [request setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
            [weakRequest cancel];
        }];

        [request setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            NSLog(@"..request successful! (result: %@)",responseObject);
            dispatch_semaphore_signal(__communicationLock);

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            if (operation.response.statusCode == 200)
                NSLog(@"..request successful! (aborted)");
            else
                NSLog(@"..request failed! (error: %@)",error);

            dispatch_semaphore_signal(__communicationLock);
        }];

        [request start];

        dispatch_semaphore_wait(__communicationLock, DISPATCH_TIME_FOREVER);
    }

    NSLog(@"Resolved URL '%@' to '%@'",originalURL,resolvedURL);

    return resolvedURL;
}
注意:由于信号量的原因,在主线程上不改变运行它不是一个好主意,因为它会阻塞主runloop(NSURLConnections讨厌这个)。因此,最好将方法调用分派到全局队列