Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve redirect of NSURL?

I have a shortened URL (e.g. t.co/12345678). I would like to resolve the redirect without having to the load the entire page via NSURLConnection. Obviously, I can get it by sending an NSURLConnection and awaiting the response but this loads the entire page first. I am only looking for the redirect URL. Is this possible? If so, how?

=============

Edit: For posterity, here is the solution, as suggested by amleszk.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:shortenedURL
                                         cachePolicy:NSURLRequestReloadIgnoringCacheData
                                     timeoutInterval:15.0f];

[request setHTTPMethod:@"HEAD"];

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                           NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
                           NSURL *resolvedURL = [httpResponse URL];
                           NSLog(@"%@", resolvedURL);
                       }];
like image 744
Keller Avatar asked Mar 31 '12 22:03

Keller


1 Answers

same method as this: finding the download size of a URL (Content-Length)

but you want the [NSURLResponse statusCode] = 301/302/303 and the Location header here: Getting "Location" header from NSHTTPURLResponse

like image 130
amleszk Avatar answered Sep 17 '22 13:09

amleszk