简体   繁体   English

如何处理NSURLRequest HTTP错误303?

[英]How to handle NSURLRequest HTTP error 303?

When I run my NSURLRequest in Cocoa, I get a 303 HTTP error, which is a redirect. 在Cocoa中运行NSURLRequest时,出现303 HTTP错误,这是重定向。 How can I pull the proper URL to redirect to? 如何提取正确的URL以重定向到? Is it in the error variable, or somewhere else? 是在错误变量中还是其他地方?

You might want to check out the automatic handling of redirects with NSURLConnection: 您可能想使用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. 这是在connection:didReceiveResponse获取它的方法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 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM