Jsoup 连接到 https(密钥库)
Jsoup connection to https(keystore)
我与 https://jizdenky.regiojet.cz/Login?0 的连接(登录)有问题。
代码:
//add certificate to trustStore
System.setProperty("javax.net.ssl.trustStore", "keystore/regionjet.jks");
Connection connection = Jsoup.connect("https://jizdenky.regiojet.cz/Login?0");
Connection.Response response = connection.data("passwordAccountCode", username).data("password", password).method(Connection.Method.POST).execute();
我仍然遇到证书路径异常
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
谁能帮帮我或者告诉我哪里出了问题?
你可以做两件事。 我只是参考这里也回答了问题。
通常允许所有证书
阅读这个答案:
对应的代码为:
TrustManager[] trustAllCertificates = new TrustManager[] {
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null; // Not relevant.
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
// Do nothing. Just allow them all.
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
// Do nothing. Just allow them all.
}
}
};
HostnameVerifier trustAllHostnames = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true; // Just allow them all.
}
};
try {
System.setProperty("jsse.enableSNIExtension", "false");
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCertificates, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(trustAllHostnames);
}
catch (GeneralSecurityException e) {
throw new ExceptionInInitializerError(e);
}
将证书添加到 JRE 的存储中
此方法要求您从 CRT
下载文件,例如你的浏览器。之后,您应该使用作为 JRE 一部分的 keytool
命令将其包含到您的 JRE 中。
完整答案在这里:
我与 https://jizdenky.regiojet.cz/Login?0 的连接(登录)有问题。
代码:
//add certificate to trustStore
System.setProperty("javax.net.ssl.trustStore", "keystore/regionjet.jks");
Connection connection = Jsoup.connect("https://jizdenky.regiojet.cz/Login?0");
Connection.Response response = connection.data("passwordAccountCode", username).data("password", password).method(Connection.Method.POST).execute();
我仍然遇到证书路径异常
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
谁能帮帮我或者告诉我哪里出了问题?
你可以做两件事。 我只是参考这里也回答了问题。
通常允许所有证书
阅读这个答案:
对应的代码为:
TrustManager[] trustAllCertificates = new TrustManager[] {
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null; // Not relevant.
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
// Do nothing. Just allow them all.
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
// Do nothing. Just allow them all.
}
}
};
HostnameVerifier trustAllHostnames = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true; // Just allow them all.
}
};
try {
System.setProperty("jsse.enableSNIExtension", "false");
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCertificates, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(trustAllHostnames);
}
catch (GeneralSecurityException e) {
throw new ExceptionInInitializerError(e);
}
将证书添加到 JRE 的存储中
此方法要求您从 CRT
下载文件,例如你的浏览器。之后,您应该使用作为 JRE 一部分的 keytool
命令将其包含到您的 JRE 中。
完整答案在这里: