开发者

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 
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜