static public void trustHttpsCertificates() throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
//Create a trust manager that does not validate certificate chains:
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
return;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
return;
}
}//X509TrustManager
};//TrustManager[]
//Install the all-trusting trust manager:
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
//SSLSocketFactory sf = sc.getSocketFactory();
//System.out.println(sf.getClass());
//System.out.println(HttpsURLConnection.getDefaultSSLSocketFactory().getClass());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
//avoid "HTTPS hostname wrong: should be <myhostname>" exception:
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) {
System.out.println("Warning: URL host '"+urlHostName+"' is different to SSLSession host '"+session.getPeerHost()+"'.");
}
return true; //also accept different hostname (e.g. domain name instead of IP address)
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}//trustHttpsCertificates
Blog Archive
-
▼
2011
(221)
-
▼
July
(26)
- javax.net.ssl.SSLException: untrusted server cert ...
- Android options menu, submenu and menu group examp...
- iSeries (AS400) V7R1 Java version and setup
- RPGLE Service Program Example - iSeries (AS400)
- Java convert hex string to byte array example
- Java convert file data to String example
- Java convert byte array to hex string example
- Apache HTTP Server sample Configuration file - htt...
- Android passing data between activities example co...
- RPGLE monitor on-error example code
- Convert data from SQL Server to DB2 using Java JDB...
- Convert Image to String and String to Image using ...
- Java convert String to Int example
- Java convert String to Date example using java.uti...
- Java convert String to double example
- Java JDBC preparedstatement example for insert, se...
- SQLRPGLE Delete Statement for db2 in iSeries (AS40...
- SQLRPGLE Update Statement for db2 in iSeries (AS4...
- SQLRPGLE Select Statement for db2 in iSeries (AS4...
- SQLRPGLE Insert Statement for db2 in iSeries (AS4...
- SQLRPGLE Create Table in db2 - Add column heading,...
- Call RPGLE from Java using Package com.ibm.as400.a...
- Call RPGLE from Java using SQL Stored Procedure
- Create Stored Procedure for RPGLE programs
- How to call Java main(String[] args) method from R...
- How to Call a Java Program from RPGLE
-
▼
July
(26)
javax.net.ssl.SSLException: untrusted server cert chain issue
When reading the content from a HTTPS connection, a javax.net.ssl.SSLException: untrusted server cert chain can be thrown for untrusted servers. To force reading from such untrusted servers, this method installs a 'all-trustung' trust manager that returns 'true' for all servers. Just call this method and install a dummy host name verifier to read data from any uncertified server.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment