How to handle NSURLRequest HTTP error 303?
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?
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
}
}
精彩评论