经过努力,我终于能够用Java编写一个简单的HTTPS服务器来处理HTTPS请求。我最终应该做的是从表单中获取数据,处理它,然后返回结果。作为一个开始,在这个简单的例子中,我只想返回"值"提交的选项。不幸的是,当我打印服务器收到的内容时,除了POST /run.html HTTP/1.1
之外,我都看不到任何POST数据。有什么问题,如何将此功能添加到其中?
PS:如果要测试,请不要忘记为SSL创建密钥库并在代码中写入信息。
我得到的POST请求是这样的(即使长度为41,我想对list=capacity.3Mbps_400RTT_PER_0.0001.txt
的缺失数据应该是正确的):
POST /run.html HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: https://localhost:8888/
Accept-Language: en-US
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:8888
Content-Length: 41
Connection: Keep-Alive
Cache-Control: no-cache
这是我在HTML中的简单表单(实际上我应该从服务器接收其HTML源代码):
<html><body>
<p>
<h3> Please select a configuration below to run the emulator.</h3>
<form action="run.html" method="POST">
<select name="list">
<option name="400" value="capacity.3Mbps_400RTT_PER_0.0001.txt">capacity.3Mbps_400RTT_PER_0.0001</option>
<option name="100" value="capacity.3Mbps_100RTT_PER_0.00001.txt">capacity.3Mbps_100RTT_PER_0.00001</option>
<option name="200" value="capacity.3Mbps_200RTT_PER_0.0001.txt">capacity.3Mbps_200RTT_PER_0.0001</option>
</select>
<input type="submit" value="Run Emulator!">
</form>
</body></html>
这是我的Java HTTPS服务器程序:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
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 HttpsServer {
public static void main(String[] args) {
String ksName = "myJKS.jks";
char ksPass[] = "key".toCharArray();
char ctPass[] = "key".toCharArray();
try {
// Runtime.getRuntime().exec("notepad");
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,0, null);
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);
// w.write(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");
//should be two new lines!!!
w.newLine();
w.newLine();
if(m.contains("GET"))
//read from file
MyFileReader(w,"index.html");
while ((m = r.readLine()) != null) {
if (m.length() == 0)
break; // End of a GET call
// w.write(m);
System.out.println(m);
w.newLine();
}
w.flush();
}
w.close();
r.close();
c.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void MyFileReader(BufferedWriter w, String uri) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(uri));
while ((sCurrentLine = br.readLine()) != null) {
w.write(sCurrentLine);
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.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());
}
}
答案 0 :(得分:0)
您如何处理服务器中的POST方法?我看到你有这个部分用于GET方法,看起来你需要为POST方法添加一个else / if语句。
if(m.contains("GET")) {
// return page HTML
} else if(m.contains("POST")) {
// process form data
// return page HTML
}