2010-07-14 115 views

回答

2

你可能想看看重定向的自動處理與NSURLConnection的:

Handling Redirects and other Request Changes

如果你想手動處理它,重定向URL是響應的「位置」標頭。您可以通過connection:didReceiveResponse委託方法獲得該方法。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; 
    // ... if the response status is 303 ... 
    if ([response respondsToSelector:@selector(allHeaderFields)]) { 
     NSString* location = [[httpResponse allHeaderFields] valueForKey:@"Location"]; 
      // do whatever with the redirect url 
    } 
} 
+0

有時我們會在頭部位置相對URL。你能建議如何處理這些?例如,我正在打開一個頁面m.espn.go.com,並在響應位置標題字段I get Location:/ index。如何處理這個。 – iProgrammer 2014-05-09 11:18:22