经过一番努力,我终于能够构建一个用Java实现的简单的HTTPS示例服务器,如下所示。它接受客户端连接并发回一段有关套接字信息的文本。所以到目前为止我有一个工作服务器示例;干净整洁。如何使其成为双向连接,以便客户端也能够以交互模式发送/接收信息?我应该如何实现后退和前进的步骤?
基本上,我希望在连接到服务器后在浏览器上显示这样的表单(这很简单;就像现在,我将发送与表单的 html 代码相对应的文本)。但我需要客户端将填写的数据发送回服务器。服务器会对这些原始数据进行一些处理,并将结果返回给客户端显示。
PS:如果您想测试该程序,请不要忘记创建 keystore 证书并提供以下信息。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.security.KeyStore;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
public class HttpsEchoer {
public static void main(String[] args) {
String ksName = "myks.jks";
char ksPass[] = "mypass".toCharArray();
char ctPass[] = "mypass".toCharArray();
try {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(ksName), ksPass);
KeyManagerFactory kmf =
KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, ctPass);
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), null, null);
SSLServerSocketFactory ssf = sc.getServerSocketFactory();
SSLServerSocket s
= (SSLServerSocket) ssf.createServerSocket(8888);
System.out.println("Server started:");
printServerSocketInfo(s);
// Listening to the port
int count = 0;
while (true) {
SSLSocket c = (SSLSocket) s.accept();
// Someone is calling this server
count++;
System.out.println("Connection #: "+count);
printSocketInfo(c);
BufferedWriter w = new BufferedWriter(
new OutputStreamWriter(c.getOutputStream()));
BufferedReader r = new BufferedReader(
new InputStreamReader(c.getInputStream()));
String m = r.readLine();
// System.out.println(m);
if (m!=null) {
// We have a real data connection
w.write("HTTP/1.1 200 OK");
w.newLine();
w.write("Content-Type: text/html");
w.newLine();
w.newLine();
w.write("<html><body><pre>");
w.newLine();
w.write("Connection #: "+count);
w.newLine();
w.newLine();
w.write(m);
w.newLine();
while ((m=r.readLine())!= null) {
if (m.length()==0) break; // End of a GET call
w.write(m);
w.newLine();
}
w.write("</pre></body></html>");
w.newLine();
w.flush();
}
w.close();
r.close();
c.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void printSocketInfo(SSLSocket s) {
System.out.println("Server socket class: "+s.getClass());
System.out.println(" Remote address = "
+s.getInetAddress().toString());
System.out.println(" Remote port = "
+s.getPort());
System.out.println(" Local socket address = "
+s.getLocalSocketAddress().toString());
System.out.println(" Local address = "
+s.getLocalAddress().toString());
System.out.println(" Local port = "
+s.getLocalPort());
}
private static void printServerSocketInfo(SSLServerSocket s) {
System.out.println("Server socket class: "+s.getClass());
System.out.println(" Socker address = "
+s.getInetAddress().toString());
System.out.println(" Socker port = "
+s.getLocalPort());
System.out.println(" Need client authentication = "
+s.getNeedClientAuth());
System.out.println(" Want client authentication = "
+s.getWantClientAuth());
System.out.println(" Use client mode = "
+s.getUseClientMode());
}
}
最佳答案
您的表单还应包含 <BUTTON type = "submit"...>
用于发送填写好的表格,以及 <FORM action="http://somesite..." method="post">
让您确定地点和方式。浏览器处理客户端(验证除外 - 您需要一些 JavaScript 代码或等效代码)。
来自 http://www.w3.org/ 的 HTML 4 或 5 规范包含很多示例。
关于java - 测试我的简单 HTTPS 服务器的客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24176569/