File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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
I agree. Here's the link:
subject: HttpsURLConnection
It's not a secret anymore!