Keller
Keller

Reputation: 17081

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);
                       }];

Upvotes: 9

Views: 3884

Answers (1)

amleszk
amleszk

Reputation: 6290

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

Upvotes: 5

Related Questions