我正在尝试让Java通过HTTPS接受所有证书。这是出于测试目的。在我收到证书未找到错误之前。但是,在我的代码之前添加以下代码后,我收到HTTPS hostname wrong: should be <sub.domain.com>
错误。问题是我的网址 IS 那个网址。我该如何解决这个问题?以下是我为解决问题而添加的代码..
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}
答案 0 :(得分:6)
您还需要设置HostNameVarifier 例如:
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
public class TrustAllHostNameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
然后
httpsConnection.setHostnameVerifier(new TrustAllHostNameVerifier ());