Apache HttpComponents ライブラリを使用して AppEngine アプリケーションに接続しています。ユーザーを認証するには、認証トークンをアプリケーションのログイン アドレス ( http://myapp.appspot.com/_ah/login?auth=.. .) に渡し、ヘッダーから Cookie を取得する必要があります。応答。しかし、ログイン ページはリダイレクト ステータス コードで応答し、HttpClient がリダイレクトに従うのを止める方法がわからないため、Cookie をインターセプトできません。
Fwiw、リクエストを送信するために使用する実際の方法は以下のとおりです。
private void execute(HttpClient client, HttpRequestBase method) {
// Set up an error handler
BasicHttpResponse errorResponse = new BasicHttpResponse(
new ProtocolVersion("HTTP_ERROR", 1, 1), 500, "ERROR");
try {
// Call HttpClient execute
client.execute(method, this.responseHandler);
} catch (Exception e) {
errorResponse.setReasonPhrase(e.getMessage());
try {
this.responseHandler.handleResponse(errorResponse);
} catch (Exception ex) {
// log and/or handle
}
}
}
クライアントがリダイレクトをたどらないようにするにはどうすればよいですか?
ありがとう。
更新:
以下の解決策に従って、作成後(およびメソッドDefaultHttpClient client
に渡す前)に次のことを行いました。execute
if (!this.followRedirect) {
client.setRedirectHandler(new RedirectHandler() {
public URI getLocationURI(HttpResponse response,
HttpContext context) throws ProtocolException {
return null;
}
public boolean isRedirectRequested(HttpResponse response,
HttpContext context) {
return false;
}
});
}
必要以上に冗長ですが、思ったほど難しくはありません。