【问题标题】:How to handle NSURLRequest HTTP error 303?如何处理 NSURLRequest HTTP 错误 303?
【发布时间】:2010-07-14 00:14:06
【问题描述】:

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

【问题讨论】:

    标签: iphone cocoa http nsurlrequest


    【解决方案1】:

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

    Handling Redirects and other Request Changes

    如果您想手动处理,重定向 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 
        }
    }
    

    【讨论】:

    • 有时我们会在位置标头中获取相对 url。你能建议如何处理这些吗?例如,我正在打开一个页面 m.espn.go.com,作为响应 Location 标头字段,我得到 Location:/index。如何处理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2023-04-09
    • 2014-12-02
    • 2019-03-24
    • 1970-01-01
    • 2013-08-02
    相关资源
    最近更新 更多