preventing HttpClient的4从以下重定向 [英] Preventing HttpClient 4 from following redirect

查看:170
本文介绍了preventing HttpClient的4从以下重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我连接到使用Apache HttpComponents库我的AppEngine上的应用程序。为了验证我的用户,我需要令牌传递下去验证应用程序的登录地址(的http://的myapp .appspot.com /%5Fah /登录?AUTH = ...)并获取一个cookie从响应的头。然而,在登录页面响应一个重定向状态code,我不知道如何停止的HttpClient从以下重定向,从而截获该cookie阻挠我。

I'm connecting to my AppEngine application using the Apache HttpComponents library. In order to authenticate my users, I need to pass an authentication token along to the application's login address (http://myapp.appspot.com/%5Fah/login?auth=...) and grab a cookie from the header of the response. However, the login page responds with a redirect status code, and I don't know how to stop HttpClient from following the redirect, thus thwarting me from intercepting the cookie.

FWIW,我用它来发送请求的实际方法如下。

Fwiw, the actual method I use to send the request is below.

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
    	}
    }
}

我将如何停止客户端从以下重定向?

How would I stop the client from following the redirect?

感谢。

更新

按照下面的解决方案,我做了以下创建一个 DefaultHttpClient客户端后,(并将它传递给前执行法):

As per the solution below, I did the following after creating a DefaultHttpClient client (and before passing it to the execute method):

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;
        }
    });
}

更详细的比它似乎它需要的,但并不困难,因为我想。

More verbose than it seems it needs to be, but not as difficult as I thought.

推荐答案

尝试使用<一个href="http://developer.android.com/reference/org/apache/http/client/RedirectHandler.html"><$c$c>RedirectHandler.这可能需要延长<一href="http://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html"><$c$c>DefaultHttpClient createRedirectHandler回报您的自定义实现()

这篇关于preventing HttpClient的4从以下重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆