0

Cocoa で NSURLRequest を実行すると、リダイレクトである 303 HTTP エラーが発生します。リダイレクト先の適切な URL を取得するにはどうすればよいですか? それはエラー変数にありますか、それとも他の場所にありますか?

4

1 に答える 1

2

NSURLConnection を使用したリダイレクトの自動処理を確認することをお勧めします。

リダイレクトおよびその他のリクエスト変更の処理

手動で処理したい場合は、リダイレクト URL が応答の「Location」ヘッダーにあります。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 
    }
}
于 2010-07-14T00:41:04.413 に答える