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
SANJAY HALDARAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
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

0
Jeffrey Dake Senior Director of TechnologyCommented:
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
0
SANJAY HALDARAuthor 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
0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

CEHJCommented:
What are you doing with 'con' such that you need to know its type?
0
SANJAY HALDARAuthor Commented:
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.
:)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Two people showed you the reason for the problem and yet you couldn't even be bothered to respond ...
0
SANJAY HALDARAuthor 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.
0
CEHJCommented:
Two people showed you the reason for the problem and yet you couldn't even be bothered to respond ...
0
SANJAY HALDARAuthor Commented:
The question was well responded that we got the solution our self, what does the last comment of CEHJ means?
0
SANJAY HALDARAuthor Commented:
ok
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.