【发布时间】:2013-05-13 15:20:48
【问题描述】:
如何退出 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方法的类。
如果我想以不同的用户名和密码登录多个用户的系统,该怎么做?
例如在同一个会话中,我想注销一个用户并使用其他用户登录。
【问题讨论】:
标签: java apache-httpclient-4.x