我需要一些帮助才能使 DIGEST Authentication 正常工作。我正在使用 apache 4.1 库。当我尝试登录时,我得到了。
线程“主”javax.net.ssl.SSLPeerUnverifiedException 中的异常:对等体未通过身份验证
我正在尝试登录 Asterisk SwitchVox Dev Extend API,您只需发送一个 xml 帖子,它就会返回信息。我当然有正确的用户名/密码,并且我在 PERL 脚本上得到了这个,但我无法在 JAVA 中得到它。
这是我的代码
public class Main {
public static void main(String[] args) throws Exception {
HttpHost targetHost = new HttpHost("192.168.143.253", 443, "https");
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("192.168.143.253", targetHost.getPort()),
new UsernamePasswordCredentials("username", "mypassword"));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate DIGEST scheme object, initialize it and add it to the local auth cache
DigestScheme digestAuth = new DigestScheme();
authCache.put(targetHost, digestAuth);
// Add AuthCache to the execution context
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
HttpGet httpget = new HttpGet("https://192.168.143.253/xml/");
System.out.println("executing request: " + httpget.getRequestLine());
System.out.println("to target: " + targetHost);
for (int i = 0; i < 3; i++) {
HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
EntityUtils.consume(entity);
}
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}