9

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);
                       }];
Laurel
  • 5,965
  • 14
  • 31
  • 57
Keller
  • 17,051
  • 8
  • 55
  • 72

1 Answers1

5

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

Community
  • 1
  • 1
amleszk
  • 6,192
  • 5
  • 38
  • 43
  • Hmm, the status code returned is 200 and the http header response does not contain the redirect URL. The response URL also only contains the original URL... – Keller Apr 01 '12 at 00:34
  • Oops, it was a caching issue. Worked like a charm, thanks a lot! – Keller Apr 01 '12 at 19:18