2012-03-31 102 views
9

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

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

编辑:对于后人,这里是解决方案,通过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); 
         }]; 

回答

5

同样的方法,这一点:finding the download size of a URL (Content-Length)

,但你想要的[NSURLResponse的StatusCode = 301/302/303和 Location头位置:Getting "Location" header from NSHTTPURLResponse

+0

嗯,返回的状态代码是200并且http标题响应不包含重定向URL。响应URL也只包含原始URL ... – Keller 2012-04-01 00:34:16

+0

糟糕,这是一个缓存问题。像魅力一样工作,非常感谢! – Keller 2012-04-01 19:18:41