What I want to do:
When I make a GET
request to a URL, the response is a 302 HTTP response
, which means its a redirect. I know that there are multiple redirects(3 to 4) after my initial GET
request. I want to get the very last URL after multiple redirects happen. I am only able to receive the first redirect URL.
What I've tried, is answers on:
- AFNetworking -- get redirect URL
- NSURLRequest: How to handle a redirected post?
- Get the last redirected url in iOS 5?
- iPhone NSURL get last redirected url
My code sniplet taken from the answers:
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];
I am able to receive the first redirected URL but there is no 2nd or 3rd redirect after that. What should I do in order to let the redirects continue and only receive the URL for the last redirect that is made. I am novice in iOS dev. I'll appreciate your help. Thanks.