• Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassCastException with HttpsURLConnection class  RSS feed

 
Dhananjeyun Rajendra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JDK 1.5's jsse.jar seems to have the HttpsURLConnection class (and some other classes) in both javax.net.ssl and com.sun.net.ssl packages. This was not so with jsse1.0.1.jar where javax.net.ssl didn't have this class, so the following code didn't have any problem during runtime:

import javax.net.ssl.*;
import com.sun.net.ssl.*;
...
URL url = new URL(urlString);
HttpsURLConnection huc = (HttpsURLConnection)url.openConnection();
...


But in the JDK1.5's jsse.jar, since javax.net.ssl has been updated with new version of this class and the one from com.sun.net.ssl has been deprecated, it throws ClassCastException during runtime.

Can anyone help?

Thanks a lot in advance.
Dhanan.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't import com.sun.net.ssl?
 
Dhananjeyun Rajendra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Don't import com.sun.net.ssl?


Sorry, I forgot to mention that importing of com.sun.net.ssl was already commented because of the ambiguities reported by the compiler, and the ClassCastException started after that (during runtime).
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, OK. Well, try catching the return value in an Object or plain URLConnection, then calling getClass().getName() on it to see what it actually is. That will probably give you a hint about what to do next.
 
Dhananjeyun Rajendra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. According to the API doc, URL object's openConnection() returns URLConnection which is a super class of HttpsURLConnection(). So the ClassCastException is a valid one in the new jsse.jar because Sun moved the HttpsURLConnection class from com.sun.net.ssl to javax.net.ssl. This altered the hierarchy structure and hence the exception. Now the question is, how to get around this?
 
Dhananjeyun Rajendra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your time, Ernest.

I managed to resolve it by using the com.sun.net.ssl package instead of javax.net.ssl, as Sun says the classes in com.sun.net.ssl are unchanged to retain backward compatibility.

Regards,
Dhanan.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!