views:

37

answers:

1

When I run my NSURLRequest in Cocoa, I get a 303 HTTP error, which is a redirect. How can I pull the proper URL to redirect to? Is it in the error variable, or somewhere else?

+1  A: 

You might want to check out the automatic handling of redirects with NSURLConnection:

Handling Redirects and other Request Changes

If you'd like to handle it manually, the redirect url is in the response's 'Location' header. Here's how you can grab it in your connection:didReceiveResponse delegate method.

- (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 
    }
}
Art Gillespie