Link to home
Start Free TrialLog in
Avatar of SANJAY HALDAR
SANJAY HALDARFlag for India

asked on

ClassCastException while trying to create HttpsURLConnection

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
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SANJAY HALDAR

ASKER

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
What are you doing with 'con' such that you need to know its type?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Two people showed you the reason for the problem and yet you couldn't even be bothered to respond ...
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.
Two people showed you the reason for the problem and yet you couldn't even be bothered to respond ...
The question was well responded that we got the solution our self, what does the last comment of CEHJ means?
ok