解决NSURL的重定向?

时间:2012-03-31 22:44:28

标签: objective-c ios nsurlconnection nsurl nsurlrequest

我有一个缩短的网址(例如t.co/12345678)。我想解决重定向而无需通过NSURLConnection加载整个页面。显然,我可以通过发送NSURLConnection并等待响应来获取它,但这会首先加载整个页面。我只是在寻找重定向网址。这可能吗?如果是这样,怎么样?

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

编辑:对于后人,这是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);
                       }];

1 个答案:

答案 0 :(得分:5)

与此相同的方法:finding the download size of a URL (Content-Length)

但你想要[NSURLResponse statusCode] = 301/302/303和 位置标题:Getting "Location" header from NSHTTPURLResponse