如何退出 HttpClient 会话?
我使用以下代码使用 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
方法的类。
如果我想用多个用户使用不同的用户名和密码登录系统,该怎么做?
例如,在同一个会话中,我想注销一个用户并使用其他用户登录。