This week's book giveaway is in the Agile forum.
We're giving away four copies of The Software Craftsman and have Sandro Mancuso on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes HttpsURLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HttpsURLConnection" Watch "HttpsURLConnection" New topic


scottie zman
Ranch Hand

Joined: Aug 08, 2001
Posts: 40
Hi, I've been struggling all day with trying to establish an https connection. I've installed Java 1.4 which includes JSSE.
When I run the code below, it gives me a ClassCastException error at the line that opens the connection. I have no problem connecting to and retrieving from non-secure sites, but this has me baffled. Any ideas / help would be greatly appreciated.

class httpsConnector {
public static void main(String[] args) throws Exception {
System.setProperty("java.protocol.handler.pkgs", "");;
String LoginURL = "";
URL login = new URL(LoginURL);
HttpsURLConnection con = (HttpsURLConnection)login.openConnection();
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
URL.openConnection() returns a URLConnection object. Your code is trying to cast that to a HttpsURLConnection object. This is illegal, because HttpsURLConnection is a subclass of URLConnection. Casting a superclass into a subclass is guaranteed to generate a runtime ClassCastException error.
Miftah Khan
- Sun Certified Programmer for the Java� 2 Platform
- Sun Certified Web Component Developer for the Java� 2 Platform, Enterprise Edition
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: HttpsURLConnection