示例代码 2:HttpsServer.javaimport java.io.*; import java.net.*; import javax.net.*; import javax.net.ssl.*; import java.security.*; import java.util.StringTokenizer;/** * This class implements a multithreaded simple HTTPS * server that supports the GET request method. * It listens on port 44, waits client requests * and serves documents. */public class HttpsServer { String keystore = "serverkeys"; char keystorepass[] = "hellothere".toCharArray(); char keypassword[] = "hiagain".toCharArray(); // The port number which the server will be listening on public static final int HTTPS_PORT = 443; public ServerSocket getServer() throws Exception { KeyStore ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream(keystore), keystorepass); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, keypassword); SSLContext sslcontext = SSLContext.getInstance("SSLv3"); sslcontext.init(kmf.getKeyManagers(), null, null); ServerSocketFactory ssf = sslcontext.getServerSocketFactory(); SSLServerSocket serversocket = (SSLServerSocket) ssf.createServerSocket(HTTPS_PORT); return serversocket; } // multi-threading -- create a new connection // for each request public void run() { ServerSocket listen; try { listen = getServer(); while(true) { Socket client = listen.accept(); ProcessConnection cc = new ProcessConnection(client); } } catch(Exception e) { System.out.println("Exception: "+e.getMessage()); } } // main program public static void main(String argv[]) throws Exception { HttpsServer https = new HttpsServer(); https.run(); } }
代码示例 1:ReadHttpsURL1import java.net.*; import javax.net.*; import javax.net.ssl.*;public class ReadHttpsURL1 { static final int HTTPS_PORT = 443; public static void main(String argv[]) throws Exception { if (argv.length != 1) { System.out.println("Usage: java ReadHttpsURL1 "); System.exit(0); } // Get a Socket factory SocketFactory factory = SSLSocketFactory.getDefault(); // Get Socket from factory Socket socket = factory.createSocket(argv[0], HTTPS_PORT); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out.write("GET / HTTP/1.0\n\n"); out.flush(); String line; StringBuffer sb = new StringBuffer(); while((line = in.readLine()) != null) { sb.append(line); } out.close(); in.close(); System.out.println(sb.toString()); } }
如果同一服务器(页面)既支持HTTP也支持HTTPS那么可以考虑用绝对URL控制建立https的安全信道,或通常的不安全信道(http)。public class ReadHttpsURL1 { public static void main(String argv[]){ URL url = new URL("https://www.sun.com"); //URL url = new URL("http://www.sun.com"); BufferedReader in String line; StringBuffer sb = new StringBuffer(); while ((line = in.readLine()) != null) {
对你想要加密的页面链接就用HTTPS
普通的页面就不用HTTPS
有兴趣的兄弟可以参考
http://www.javaidea.net/list.jsp?topic=26
和
http://www.javaidea.net/list.jsp?topic=27
这两篇文章已经说的很详细,只是在实现的时候遇到一些问题,目前客户端的代码已经调通了,服务端的现在有“Exception: Address already in use: JVM_Bind”的错误,正在调
import java.net.*;
import javax.net.*;
import javax.net.ssl.*;
import java.security.*;
import java.util.StringTokenizer;/**
* This class implements a multithreaded simple HTTPS
* server that supports the GET request method.
* It listens on port 44, waits client requests
* and serves documents.
*/public class HttpsServer { String keystore = "serverkeys";
char keystorepass[] = "hellothere".toCharArray();
char keypassword[] = "hiagain".toCharArray(); // The port number which the server will be listening on
public static final int HTTPS_PORT = 443;
public ServerSocket getServer() throws Exception { KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(keystore), keystorepass);
KeyManagerFactory kmf =
KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, keypassword);
SSLContext sslcontext =
SSLContext.getInstance("SSLv3");
sslcontext.init(kmf.getKeyManagers(), null, null);
ServerSocketFactory ssf =
sslcontext.getServerSocketFactory();
SSLServerSocket serversocket = (SSLServerSocket)
ssf.createServerSocket(HTTPS_PORT);
return serversocket; }
// multi-threading -- create a new connection
// for each request
public void run() {
ServerSocket listen;
try {
listen = getServer();
while(true) {
Socket client = listen.accept();
ProcessConnection cc = new
ProcessConnection(client);
}
} catch(Exception e) {
System.out.println("Exception: "+e.getMessage());
}
} // main program
public static void main(String argv[]) throws Exception {
HttpsServer https = new HttpsServer();
https.run();
}
}
import javax.net.*;
import javax.net.ssl.*;public class ReadHttpsURL1 {
static final int HTTPS_PORT = 443; public static void main(String argv[]) throws Exception {
if (argv.length != 1) {
System.out.println("Usage: java ReadHttpsURL1 ");
System.exit(0);
} // Get a Socket factory
SocketFactory factory = SSLSocketFactory.getDefault(); // Get Socket from factory
Socket socket = factory.createSocket(argv[0], HTTPS_PORT); BufferedWriter out
= new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in
= new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.write("GET / HTTP/1.0\n\n");
out.flush(); String line;
StringBuffer sb = new StringBuffer();
while((line = in.readLine()) != null) {
sb.append(line);
}
out.close();
in.close();
System.out.println(sb.toString());
}
}
public static void main(String argv[]){ URL url = new URL("https://www.sun.com");
//URL url = new URL("http://www.sun.com");
BufferedReader in
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
}
in.close();
System.out.println(sb.toString());
}
}