我正在尝试为 Java 构建自己的嵌入式 HTTP 服务器,并针对内部封闭系统进行非常具体的使用。使用已经存在的嵌入式解决方案意味着我需要将它们剥离以针对我的内部系统的特定用例进行重新安装。
我已设法让我的 Java HTTPD 从 Web 浏览器接收 HTTP 请求,但 HTTPD 接收的请求不完整。
下面是服务器线程代码(假装 ServerSocket 工作得很好):
public class HttpThread implements Runnable {
private Socket socket;
private DataInputStream in;
private PrintStream out;
private BufferedReader br;
private String EOF = "\n";
private String STATUS_200 = "HTTP/1.1 200 OK" + EOF;
public HttpThread(Socket socket) throws IOException {
this.socket = socket;
this.in = new DataInputStream(this.socket.getInputStream());
this.out = new PrintStream(new BufferedOutputStream(this.socket.getOutputStream()));
}
@Override
public void run() {
System.out.println("New thread...");
try {
processInput();
//socket.close();
} catch (IOException ex) {
Logger.getLogger(HttpThread.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//System.out.println("Thread END...");
}
private String processInput() throws IOException {
String line;
StringBuilder buff = new StringBuilder();
br = new BufferedReader(new InputStreamReader(in));
while ((line = br.readLine()) != null) {
buff.append(line);
System.out.println(">>> " + line);
if (line.trim().isEmpty()) {
break;
}
}
out.print(STATUS_200);
out.print("Server: Test Server\n" +
"Content-Type: text/html; charset=UTF-8\n" +
"Connection: close");
out.print(EOF);
out.print("<html><body>yello</body></html>");
out.print(EOF);
out.flush();
System.out.println(STATUS_200);
return buff.toString();
}
}
我正在使用此 HTML 脚本来测试服务器线程以模拟 POST 请求:
<html>
<body onLoad="document.test.submit();">
<form action="http://localhost:9999/" method="POST" name="test">
<input type=hidden name="userid" value="1443"/>
<input type=hidden name="password" value="1443"/>
</form>
</body>
</html>
当我使用浏览器调用 HTML 代码时,Java HTTPD 收到不完整的响应:
New thread...
>>> POST / HTTP/1.1
>>> Host: localhost:9999
>>> Connection: keep-alive
>>> Content-Length: 25
>>> Cache-Control: max-age=0
>>> Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
>>> Origin: null
>>> User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36
>>> Content-Type: application/x-www-form-urlencoded
>>> Accept-Encoding: gzip,deflate,sdch
>>> Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
>>>
HTTP/1.1 200 OK
New thread...
>>> GET /favicon.ico HTTP/1.1
>>> Host: localhost:9999
>>> Connection: keep-alive
>>> Accept: */*
>>> User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36
>>> Accept-Encoding: gzip,deflate,sdch
>>> Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
>>>
HTTP/1.1 200 OK
HTTPD 似乎只接收了 HTTP header ,而停止接收 POST 正文。 我可以知道一些解决方案来解决上述问题吗?
谢谢。
最佳答案
HTTP header 和正文由空行分隔( also see the HTTP RFC ,尤其是“5 Request”章节)。您的服务器读取套接字 Inputstream
但在空行处中断:
if (line.trim().isEmpty()) {
break;
}
因此显然你不会收到尸体。您应该完全使用 Inputstream
。
除此之外,我建议您使用现有的解决方案。有大量的 HTTP 服务器实现,它们在现实世界的使用中经过了充分的测试和验证。使用现有的轻量级服务器可以省去很多麻烦,例如Jetty、Netty 或类似的。
关于Java HTTP 服务器响应不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18012331/