File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes HttpsURLConnection ClassCastException Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "HttpsURLConnection ClassCastException Question" Watch "HttpsURLConnection ClassCastException Question" New topic
Author

HttpsURLConnection ClassCastException Question

Zabe Agha
Greenhorn

Joined: Feb 27, 2003
Posts: 19
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?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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 public, 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


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Zabe Agha
Greenhorn

Joined: Feb 27, 2003
Posts: 19
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

Joined: Feb 27, 2003
Posts: 19
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

Joined: Jan 30, 2002
Posts: 3451
Terrific! There always seems to be a solution if we dig deep enough.
Michael Morris
Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 68
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
sabina farah
Greenhorn

Joined: Nov 20, 2011
Posts: 1
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.
kelven cheung
Greenhorn

Joined: Dec 14, 2011
Posts: 1
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 ?
 
GeeCON Prague 2014
 
subject: HttpsURLConnection ClassCastException Question