[英]Test client for my simple HTTPS server
经过大量的努力,我终于能够构建一个用Java实现的简单HTTPS示例服务器,如下所示。 它接受客户端连接,并发送回有关套接字信息的一段文本。 因此,到目前为止,我有一个有效的服务器示例; 整洁。 如何以交互方式使客户端能够发送/接收信息的双向连接? 我应该如何执行前进和后退的步骤?
基本上,我希望在连接到服务器后在浏览器中显示这样的表单(这很容易;就像现在,我将发送与表单的html代码相对应的文本)。 但是我需要客户端将填写好的数据发送回服务器。 服务器将对这些原始数据进行一些处理,然后将结果返回给客户端。
PS:如果要测试程序,请不要忘记创建密钥库证书并提供以下信息。
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规范包含许多示例。
您可能需要使服务器以JSON字符串或类似形式发送响应,以便客户端接收该响应,解析结果并相应地显示信息。 这是我接收服务器响应(以JSON字符串的形式)然后打印结果的示例(我懒得将其翻译成英语):
package com.arielnmz;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import com.google.gson.Gson;
public class JavaHttpRequestJSON {
public static void main(String[] args) throws Exception {
while (true) {
new JavaHttpRequestJSON().testGet(new java.util.Random().nextInt());
Thread.sleep(2000);
}
}
private void testGet(int valor) throws Exception {
// Crear URL
URL url = new URL("http://localhost/http_req_test/server_script_json.php");
// Establecer conexión
URLConnection conexion = url.openConnection();
// Enviaremos información
conexion.setDoOutput(true);
// Recibiremos respuesta
conexion.setDoInput(true);
// Establecer timeouts
conexion.setConnectTimeout(5000);
conexion.setReadTimeout(5000);
// Usar caches: NO
conexion.setUseCaches(false);
conexion.setDefaultUseCaches(false);
// Le decimos al servidor qué y cómo estamos enviando
conexion.setRequestProperty("Content-Type", "application/json");
// Creamos una instancia de nuestro "codificador" JSON
String json;
Gson gson = new Gson();
// Creamos un objeto y lo codificamos como JSON
// Map<String, String> map = new HashMap<String, String>();
// map.put("valor", Integer.toString( num ) );
ObjetoEnvio objetoEnvio = new ObjetoEnvio();
objetoEnvio.setValor(valor);
json = gson.toJson(objetoEnvio);
System.out.println("Objeto en JSON: "+json+" / valor enviado "+objetoEnvio.getValor());
DataOutputStream os = new DataOutputStream(conexion.getOutputStream());
os.writeBytes(json);
os.close();
StringBuffer respuesta = new StringBuffer();
BufferedReader in = new BufferedReader(
new InputStreamReader( conexion.getInputStream() )
);
String linea;
while ((linea = in.readLine()) != null) {
respuesta.append(linea);
}
in.close();
ObjetoRespuesta respuesta_json = gson.fromJson(respuesta.toString(), ObjetoRespuesta.class);
// System.out.println(respuesta.toString());
System.out.println(respuesta_json.getMensaje());
System.out.println("Conexión finalizada");
}
private class ObjetoEnvio {
private int valor;
public void setValor(int valor) {
this.valor = valor;
}
public int getValor() {
return valor;
}
}
private class ObjetoRespuesta {
private String mensaje;
public String getMensaje() { return mensaje; }
}
}
更新 :
该答案基于以下方法:
action
和method
属性将数据作为url编码的字符串提交。 例如:
<form method="POST" action="process.php">
"{ 'response' : 1 }";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.