0

当我在 Cocoa 中运行我的 NSURLRequest 时,我得到一个 303 HTTP 错误,这是一个重定向。如何获取正确的 URL 以重定向到?它在错误变量中,还是在其他地方?

4

1 回答 1

2

您可能想查看使用 NSURLConnection 自动处理重定向:

处理重定向和其他请求更改

如果您想手动处理它,重定向 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 
    }
}
于 2010-07-14T00:41:04.413 回答