2010-07-14 71 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