Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ClassCastException..trying to use HttpsURLConnection

user13174321Mar 14 2013 — edited Mar 15 2013
Hi, I'm trying to make an HTTPS connection using

Test and Prod environment configuration.
----------------------------------------------------------
jdk1.6.0_32
Linux

In the Test environment I am able to get a connection properly. But in the Prod env I am getting the Exception

Caused by: java.lang.ClassCastException: com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl cannot be cast to javax.net.ssl.HttpsURLConnection

the url.openConnection() returns the following objects in two envs.

Prod:
com.sun.net.ssl.internal.www.protocol.https.DelegateHttpsURLConnection

SIT:
sun.net.www.protocol.https.DelegateHttpsURLConnection


code :
-----------------------------------------------------------------------------------------------
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;


import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class WebAppAdaptor {

public static void main(String args[]) {

HttpsURLConnection con = null;
try {
URL url = new URL(PropertyManager.getProperty("https://www.google.com");
SSLContext ssl_ctx = SSLContext.getInstance("TLS");
TrustManager[] trust_mgr = get_trust_mgr();
ssl_ctx.init(null, // key manager
trust_mgr, // trust manager
new SecureRandom()); // random number generator
HttpsURLConnection.setDefaultSSLSocketFactory(ssl_ctx.getSocketFactory());

con = (HttpsURLConnection) url.openConnection();

}catch(Exception e){
e.printstacktrace();
}
}
}

how to avoid this exception. Is this some configuration i am missing.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 12 2013
Added on Mar 14 2013
3 comments
3,320 views