• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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
0
SANJAY HALDAR
Asked:
SANJAY HALDAR
  • 5
  • 4
3 Solutions
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now