We help IT Professionals succeed at work.

ClassCastException while trying to create HttpsURLConnection

SANJAY HALDAR
SANJAY HALDAR asked
on
Hi,
when I am trying to create httpsURLConnection like this:
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

I am getting ClassCastException but its working fine in standalone application

JDK:1.4
Weblogic: 8


CODE SAMPLE:
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
-----
----

            URL url;
            try {

                  SSLContext ssl_ctx = SSLContext.getInstance("TLS");
                  TrustManager[] trust_mgr = get_trust_mgr();
                  ssl_ctx.init(null, trust_mgr,new SecureRandom()); // random number generator
                  HttpsURLConnection.setDefaultSSLSocketFactory(ssl_ctx
                              .getSocketFactory());

                  url = new URL("https://.......");
                  
                  HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

}catch(.....){
----
}

Please Help me to resolve this problem
 Thanks you
Comment
Watch Question

Top Expert 2016
Commented:
CCE means it's not the class you think it is. Probably one of Weblogic's. Test with

Object o = url.openConnection();
System.out.printf("Connection is of type %s%n", o.getClass());

Open in new window

Jeffrey Dake Senior Director of Technologyy
Commented:
I believe weblogic has its own implementation of httpsurlconnection. You probably need to use that instead. http://docs.oracle.com/cd/E13222_01/wls/docs81/javadocs/weblogic/net/http/HttpsURLConnection.html

Found a similar report here:
https://community.oracle.com/thread/1066325?start=0&tstart=0

Author

Commented:
Thanks for your responses.

I am able to resolve this. Please find updated code:

import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
-----
----

            URL url;
            try {

                  SSLContext ssl_ctx = SSLContext.getInstance("TLS");
                  TrustManager[] trust_mgr = get_trust_mgr();
                  ssl_ctx.init(null, trust_mgr,new SecureRandom()); // random number generator
                  HttpsURLConnection.setDefaultSSLSocketFactory(ssl_ctx
                              .getSocketFactory());

                             .getSocketFactory());

                  //url = new java.net.URL("https://......");
                  url = new URL(null,"https://....",new sun.net.www.protocol.https.Handler());
                  
                      URI uri=null;
                  try {
                        uri = new URI(url.getProtocol(),url.getAuthority(), url.getPath(), url.getQuery(), null);
                  } catch (URISyntaxException e) {
                        e.printStackTrace();
                  }
                      
                      url = uri.toURL();

                 
                  HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

}catch(.....){
----
}





Regards,
Ajaya Samanta
Top Expert 2016

Commented:
What are you doing with 'con' such that you need to know its type?
I've requested that this question be closed as follows:

Accepted answer: 0 points for SANJAY HALDAR's comment #a40646180

for the following reason:

url = new java.net.URL("https://......")
Object o=url.openConnection()
If we print, in standalone application we will get "sun.net.www.protocol.https.DelegateHttpsURLConnection"
where as in weblogic we will get "weblogic.net.http.SOAPHttpsURLConnection" as a return type

if we explicitly want to type cast into (javax.net.ssl.HttpsURLConnection) it will work in standalone application but in server it will throw ClassCastException because we can't cast weblogic.net.http.SOAPHttpsURLConnection to javax.net.ssl.HttpsURLConnection.


In case of
url= new URL(null, spec1,new sun.net.www.protocol.https.Handler());
Object o=url.openConnection()
If we print 'o' in standalone application and in server we will get sun.net.www.protocol.https.DelegateHttpsURLConnection as a response. And sun.net.www.protocol.https.DelegateHttpsURLConnection can be cast to javax.net.ssl.HttpsURLConnection
i.e. javax.net.ssl.HttpsURLConnection con=(javax.net.ssl.HttpsURLConnection)o;

Regards.
:)
Top Expert 2016

Commented:
Two people showed you the reason for the problem and yet you couldn't even be bothered to respond ...

Author

Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for SANJAY HALDAR's comment #a40646513

for the following reason:

Provided solution worked out and solved the issue.

Thanks.
Top Expert 2016

Commented:
Two people showed you the reason for the problem and yet you couldn't even be bothered to respond ...

Author

Commented:
The question was well responded that we got the solution our self, what does the last comment of CEHJ means?

Author

Commented:
ok