【发布时间】:2014-06-17 04:56:11
【问题描述】:
我正在使用 Java 中的 Apache HttpClient 4.3.4 库以编程方式向需要身份验证的 URL 提供凭据。这就是我的代码中的内容:
public static void clientAuthentication() throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials("username", "password"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("http://www.example.com");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
for (Header h : response.getAllHeaders()){
System.out.println(h.toString());
}
EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
我能够成功通过 URL 进行身份验证,因为我收到了 200 状态代码。我的问题是,既然我已经以编程方式“登录”了,有没有办法以编程方式“注销”?此 URL 没有任何“注销”按钮或类似的东西;所以我想知道如何以编程方式注销。
谢谢。
【问题讨论】:
标签: java http authentication httpclient apache-httpclient-4.x