退出 HttpClient 会话 [英] Exit from HttpClient session

查看:40
本文介绍了退出 HttpClient 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何退出 HttpClient 会话?

How to exit from HttpClient session?

我使用以下代码通过 Apache HttpClient 登录应用程序

I use the following code to login to the application using Apache HttpClient

public HttpClient loginToHexgen(String username, String password) {
        HttpClient client = new DefaultHttpClient();

        // send post url to login to  hexgen
        HttpPost post = new HttpPost("http://localhost:8080/j_spring_security_check");

        try {
            // set the user name and password
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("j_username", username));
            nameValuePairs.add(new BasicNameValuePair("j_password", password));

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(post);

            HttpEntity entity = response.getEntity();

            if (entity != null) {
                post.abort();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return client;
    }

如下:

HttpClient client = new DefaultHttpClient();
client= httpRequest.loginToHexgen("mayank", "hexgen");

这里的 httpRequest 是使用 loginToHexgen 方法的类.

here httpRequest is the class where the loginToHexgen method is used.

如果我想用不同的用户名和密码以多个用户登录系统,该怎么做?.

If I want to login to the system with multiple user with diffrent user name and password how to do this?.

例如在同一个会话中,我想注销一个用户并使用其他用户登录.

Like for example in the same session I want to logout one user and login using other user.

推荐答案

您可以使用一种解决方法 – 使用新的 cookieStore 向新用户发送请求.

You may use a workaround – to send a request to a new user with a new cookieStore.

// Create a local instance of cookie store
cookieStore = new BasicCookieStore();
// Set the store
httpClient.setCookieStore(cookieStore);

服务器将为您的新用户打开一个新会话.请注意,旧会话不会关闭.我不建议使用这种方式.

Server will open a new session to your new user. Please note that the old session will NOT be closed. I do not recommend to use this way.

会话管理是在服务器端进行的——你不能在客户端进行.我建议在测试结束时您应该调用服务器 URL,这将使服务器端的会话无效.(一般使用表单认证的应用都有注销功能,你只需要使用它即可)

The session management is performed on the server side – you can not do it on the client side. I recommend in the end of your test you should call to a server URL that will invalidate a session on the server side. (Generally applications that use Form authentication have a logout functionality and you just need to use it)

这篇关于退出 HttpClient 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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