• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

HttpsURLConnection ClassCastException Question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having an issue with my code where, when I have the following:
HttpsURLConnection aHttpsURLConnection = null;
URL aURL = new URL("https://my.machine.com/xml/xps.cgi");
aHttpsURLConnection = (HttpsURLConnection)aURL.openConnection();
I get a ClassCastException. However, if I use a normal HttpURLConnection, I don't get the exception! Why?
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aurangzeb,
I can only speculate on this, since I have done very little personally with HttpURLConnection and HttpsURLConnection, but there may be a clue in the javadocs for URL.openConnection():
If for the URL's protocol (such as HTTP or JAR), there exists a coderanch, specialized URLConnection subclass belonging to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. For example, for HTTP an HttpURLConnection will be returned, and for JAR a JarURLConnection will be returned.

There may be also be a certificate problem with the SSL on your server. Assuming you are using something like Tomcat, you'll have to configure to run SSL since the default configuration doesn't support it.
Sorry I can't be more help.

Michael Morris
 
Zabe Agha
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx for the suggestion Michael. I think you're on the right track.
I figured out that the reason that I'm getting the ClassCastException is that even though I have:
URL aURL = new URL("https://my.machine.com/xml/xps.cgi");
My:
aURL.openConnection()
is returning a HttpURLConnection, and the cast to HttpsURLConnection is causing the prob.
The confusing thing is this: Does that mean that even though HttpsURLConnection extends HttpURLConnection, it doesn't qualify as a valid return type for URL.openConnection() because it's in the javax.net.ssl package, which does not belong "to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net"?
If so, then what alternative do I have to pass a https URL to URL, and get back a secure, HttpsURLConnection?
I tried the following test:
URL aURL = new URL("https://www.verisign.com");
URLConnection urlConn = aURL.openConnection();
if (urlConn instanceof com.sun.net.ssl.HttpsURLConnection) {
System.out.println("*** openConnection returns an instanceof com.sun.net.ssl.HttpsURLConnection");
}

if (urlConn instanceof javax.net.ssl.HttpsURLConnection) {
System.out.println("*** openConnection returns an instanceof javax.net.ssl.HttpsURLConnection");
}
if (urlConn instanceof HttpURLConnection) {
System.out.println("*** openConnection returns an instnace of HttpURLConnection");
}
And my output is:
*** openConnection returns an instanceof com.sun.net.ssl.HttpsURLConnection
*** openConnection returns an instnace of HttpURLConnection
Doesn't that seem like a mistake? Why is URL.openConnection() returning a deprecated class?
Furthermore, what do I need to do to get a HttpsURLConnection?
[ February 28, 2003: Message edited by: Aurangzeb Agha ]
 
Zabe Agha
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Solution found!
After busting my head on this even further over the weekend, I found the following solution to this problem:
http://forum.java.sun.com/thread.jsp?thread=196586&forum=2&message=648727
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Terrific! There always seems to be a solution if we dig deep enough.
Michael Morris
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am also facing the same problem and while finding the solution i come across this thread.
URL mention is not working, can you please check the URL.
http://forum.java.sun.com/thread.jsp?thread=196586&forum=2&message=648727
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 2 things that cause class cast error in this situation
1)In case the routing url is an "http" url instead of "https" usrl
2)In case you have imported "sun" packages in your import statements or have explicitly used sun classes in the code.

Ensure that you are testing with an "https" url and remove any reference to sun.net.* from your import stmts.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zabe Agha wrote:Solution found!
After busting my head on this even further over the weekend, I found the following solution to this problem:
http://forum.java.sun.com/thread.jsp?thread=196586&forum=2&message=648727



hi, can you share the solution ?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic