1

schema://hostname の形式の URL があります (例: https://some.server.com )。
リダイレクト ページ (私の場合はログイン フォーム) を取得しようとしています (例: httpx://some.server.com/this/is/you/loginpage)

ブラウザは、応答の「Location」ヘッダーによってサーバーから受信したこのページに自動的にリダイレクトします。Google Chrome でこのリクエストを分析すると、ロケーション ヘッダーに「302」ステータスが表示されます。このロケーション ヘッダーには正しい URL がありますが、何らかの理由で、NSURLConnectionDelegate メソッドを使用してこのヘッダー (または http ステータス!) を取得できません。AFNetworking も試しましたが、結果は同じでした。

この場合、connection:willSendRequest:redirectResponse: メソッドは正規の変更に対してのみ呼び出され、場所の変更に対しては呼び出されません。

また、UIWebView DOES がこの「場所」URL に自動的にリダイレクトすることもわかりました。また、UIWebView デリゲート メソッドを使用してこのリダイレクトをキャッチすることもできます: webView:shouldStartLoadWithRequest:navigationType: UIWebView は使用したくありません。バックグラウンド操作。(ただし、現在は回避策として使用しています)。

望ましい動作については、ここでも説明されています: http://en.wikipedia.org/wiki/HTTP_location NSURLConnection (または任意の AFNetworking クラス) を使用してこのリダイレクトを検出/実行する方法はありますか?

NSURLConnection を使用した関連コード (この状況ではリダイレクトしません):

- (void)resolveUsingNSURLConnection:(NSURL *)URL {
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    self.isRequestBusy = YES;
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (urlConnection) {
        [self waitUntilRequestFinished];
    }

    NSLog(@"resolveUsingNSURLConnection ready (status: %i, URL: %@)", self.response.statusCode, self.response.URL);
}

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse {
    if (redirectResponse) {
        // Just log the redirect request
        NSLog(@"[%@] server redirect allowed:\n\t%@ %@\n\t%@ %@", NSStringFromClass([self class]), self.request.HTTPMethod, self.request.URL.absoluteString, request.HTTPMethod, request.URL.absoluteString);
        return request;
    } else {
        // Just log the canonical change
        NSLog(@"[%@] canonical change:\n\t%@ %@\n\t%@ %@", NSStringFromClass([self class]), self.request.HTTPMethod, self.request.URL.absoluteString, request.HTTPMethod, request.URL.absoluteString);
        return request;
    }
}

UIWebView を使用した関連コード (目的のコンポーネントではなく、目的のリダイレクト動作があります):

- (void)resolveUsingUIWebView:(NSURL *)URL {
    if (![NSThread isMainThread]) {
        [self performSelectorOnMainThread:@selector(resolveUsingUIWebView:) withObject:URL waitUntilDone:YES];
        return;
    }

    NSURLRequest *hostnameURLRequest = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];
    self.isRequestBusy = YES;
    UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectZero];
    webview.delegate = self;
    [webview loadRequest:hostnameURLRequest];
    [self waitUntilRequestFinished];

    NSLog(@"resolveUsingUIWebView ready (status: UNKNOWN, URL: %@)", webview.request.URL);
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"webView:shouldStartLoadWithRequest: %@ (%i)", request.URL, navigationType);
    return YES;
}
4

1 に答える 1

2

UIWebView がリダイレクトを異なる方法で処理する方法はいくつかありますが、いくつかは次のとおりです。

Web サービスは User-Agent 文字列を調べて、異なる動作をする場合があります。

リダイレクトは、JaveScript の場合があります。

于 2013-11-11T21:36:26.670 に答える