• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpsURLConnection

 
scottie zman
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic