aspose file tools*
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
Author

HttpsURLConnection

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.
Thanks
Zman

import java.net.*;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;
class httpsConnector {
public static void main(String[] args) throws Exception {
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
String LoginURL = "https://mySecureSite.com";
URL login = new URL(LoginURL);
HttpsURLConnection con = (HttpsURLConnection)login.openConnection();
}
}
Anonymous
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.
Regards,
------------------
Miftah Khan
- Sun Certified Programmer for the Java� 2 Platform
- Sun Certified Web Component Developer for the Java� 2 Platform, Enterprise Edition
 
jQuery in Action, 2nd edition
 
subject: HttpsURLConnection
 
Similar Threads
Communicating with HTTPS server with Client Authentication
going mad about HttpsURLConnection
Https url connection ArrayIndeoutofBounds Exception java
HTML from HTTPS page
Getting error while posting HTTPS POST method