This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes HTTPS over JSSE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTTPS over JSSE" Watch "HTTPS over JSSE" New topic
Author

HTTPS over JSSE

Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
Sorry to post this here.. This is not strictly JSP/Servlet but hope to get an answer here (failed to get one anywhere else)
I need to download a file over an https connection.. How do I go about this ?
This code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.security.* ;
import javax.net.* ;
public class GetFile {
public static void main(String[] arguments) {
if (arguments.length == 1) {
PageFrame page = new PageFrame(arguments[0]);
page.show();
} else
System.out.println("Usage: java GetFile url");
}
}
class PageFrame extends JFrame {
JTextArea box = new JTextArea("Getting data ...");
URL page;
public PageFrame(String address) {
super(address);
setSize(600, 300);
JScrollPane pane = new JScrollPane(box);
getContentPane().add(pane);
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
};
addWindowListener(l);
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

try
{
Class clsFactory = Class.forName("com.sun.net.ssl.internal.ssl.Provider");
if( (null != clsFactory) && (null == Security.getProvider("SunJSSE")) )
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
}
catch( ClassNotFoundException cfe )
{
// throw new Exception("Unable to load the JSSE SSL stream handler." + "Check classpath." + cfe.toString());
System.out.println("Exception 1") ;
}

catch (Exception e) {}

try {
page = new URL(address);
getData(page);
} catch (MalformedURLException e) {
System.out.println("Bad URL: " + address);
}
}
void getData(URL url) {
URLConnection conn = null;
InputStreamReader in;
BufferedReader data;
String line;
StringBuffer buf = new StringBuffer();
try {
conn = this.page.openConnection();
conn.connect();
box.setText("Connection opened ...");
in = new InputStreamReader(conn.getInputStream());
data = new BufferedReader(in);
box.setText("Reading data ...");
while ((line = data.readLine()) != null)
buf.append(line + "\n");
box.setText(buf.toString());
} catch (IOException e) {
System.out.println("IO Error:" + e.getMessage());
}
}
}

gives me
C:\Program Files\JCreator\MyProjects\Mak>java GetFile https://localhost:8443/tes
t.jsp
Exception in thread "main" java.lang.NoClassDefFoundError: javax/net/SocketFacto
ry
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at com.sun.net.ssl.HttpsURLConnection.<clinit>([DashoPro-V1.2-120198])
at com.sun.net.ssl.internal.www.protocol.https.Handler.openConnection([D
ashoPro-V1.2-120198])
at java.net.URL.openConnection(Unknown Source)
at PageFrame.getData(GetFile.java:66)
at PageFrame.<init>(GetFile.java:53)
at GetFile.main(GetFile.java:12)

It works for http.. https fails.. can anyone help me please ? Its very urgent.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: HTTPS over JSSE
 
Similar Threads
Need to download a file
HTTPSURLConnect and JSSE
HTTPSURLConnection and JSSE
Download a file via https
HTTPSURLConnection/JSSE