获取重定向网址

时间:2013-06-28 11:39:35

标签: objective-c

我有一个我知道的网址会将我重定向到另一个目的地:

url = [NSString stringWithFormat:@"http://graph.facebook.com/%d/picture?type=normal", profile_id];

在这种情况下,http://profile.ak.fbcdn.net/static-ak/rsrc.php/v1/yh/r/C5yt7Cqf3zU.jpg

如何知道第一个网址的第二个网址?

1 个答案:

答案 0 :(得分:2)

您可以通过以下代码找到:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    // cast the response to NSHTTPURLResponse so we can look for 404 etc
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSURL *url = [httpResponse URL];

    if ([httpResponse statusCode] >= 400) {
        // do error handling here
        NSLog(@"remote url returned error %d %@",[httpResponse statusCode],[NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]]);
    } else {
        // start recieving data
    }
}

感谢。