我使用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();
}
}
我能够成功验证网址,因为我获得了200状态代码。我的问题是,现在我已经登录了"以编程方式,是否有办法退出"编程?此网址没有任何"退出"按钮或类似的东西;所以我想知道如何以编程方式注销。
感谢。
答案 0 :(得分:2)
退出我不这么认为。你可以在httpclient中使用timeout方法。
HttpConnectionParams.setConnectionTimeout(httpParams, 30 * 1000);
答案 1 :(得分:0)
应该有一个API端点要注销。你需要打电话。
答案 2 :(得分:0)
我认为HTTPS BASIC没有注销会话的机制。 (我假设您的服务器使用BASIC身份验证)。
rfc表示会话有效,直到收到下一个身份验证质询。因此,一种丑陋的方式是使用不正确的凭据进行另一次auth尝试。但是在你这样做之前要考虑一下,因为太多不正确的auth尝试可能让人们认为你是在破坏密码。