BufferedReader 未读取请求的 body
BufferedReader is not reading body of request
我试图从 HttpPost 读取数据,但是当我从 BufferedReader 读取数据时,我只得到 header 信息。请看下面的代码。
这是服务器
try {
ServerSocket server = new ServerSocket(8332);
System.out.println("Listening for connection on port 8332 ....");
while (true) {
try (Socket socket = server.accept()) {
BufferedReader buffer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String request = "";
String line;
while ((line = buffer.readLine()) != null) {
System.out.println(line);
request += line;
}
System.out.print("port 8332 reading: " + request);
} catch (IOException e) {
System.out.print(e.getMessage());
}
}
} catch (IOException e){
System.out.print(e.getMessage());
}
这是客户
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("http://localhost:8332");
try {
StringEntity params =new StringEntity("details={\"name\":\"myname\",\"age\":\"20\"} ");
post.addHeader("content-type", "application/x-www-form-urlencoded");
post.setEntity(params);
client.execute(post);
} catch (IOException e){
System.out.println(e);
}
当我运行这个程序时,我得到以下输出
Listening for connection on port 8332 ....
POST / HTTP/1.1
content-type: application/x-www-form-urlencoded
Content-Length: 37
Host: localhost:8332
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_131)
Accept-Encoding: gzip,deflate
经过调试,程序似乎没有退出这个 while 循环
while ((line = buffer.readLine()) != null) {
System.out.println(line);
request += line;
}
但我不明白为什么。请帮助我卡了一整天。
提前致谢
在 while 循环中使用 !=0 而不是 !=null:
while((line = buffer.readLine()).length() !=0) {
输出:
port 8332 reading: POST / HTTP/1.1content-type:
application/x-www-form-urlencodedContent-Length: 18Host:
localhost:8332Connection: Keep-AliveUser-Agent:
Apache-HttpClient/4.5.3 (Java/1.8.0_171)Accept-Encoding:
gzip,deflateorg.apache.http.NoHttpResponseException: localhost:8332
failed to respond
But I can't figure out why.
您的服务器从 buffer.readLine()
获得 null
的唯一方法是客户端关闭其套接字输出流。
问题是客户端试图保持连接...这意味着它不会关闭其套接字输出流。这意味着服务器需要尊重“content-length”header;即计算读取的字节数而不是查找 end-of-stream.
从根本上说,您的服务器端没有正确实施 HTTP 1.1 规范。
怎么办?
好吧我的建议是不要尝试从套接字开始实现HTTP。使用现有框架...或 Apache HttpComponents 库。
但如果您坚持这样做1,请在开始尝试实施之前通读 阅读 HTTP 规范。并在您 运行 遇到实施问题时 随时 查阅规范,以检查您是否在做正确的事情。
1 - 定义:受虐狂 - 享受看似痛苦或乏味的 activity。
我试图从 HttpPost 读取数据,但是当我从 BufferedReader 读取数据时,我只得到 header 信息。请看下面的代码。
这是服务器
try {
ServerSocket server = new ServerSocket(8332);
System.out.println("Listening for connection on port 8332 ....");
while (true) {
try (Socket socket = server.accept()) {
BufferedReader buffer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String request = "";
String line;
while ((line = buffer.readLine()) != null) {
System.out.println(line);
request += line;
}
System.out.print("port 8332 reading: " + request);
} catch (IOException e) {
System.out.print(e.getMessage());
}
}
} catch (IOException e){
System.out.print(e.getMessage());
}
这是客户
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("http://localhost:8332");
try {
StringEntity params =new StringEntity("details={\"name\":\"myname\",\"age\":\"20\"} ");
post.addHeader("content-type", "application/x-www-form-urlencoded");
post.setEntity(params);
client.execute(post);
} catch (IOException e){
System.out.println(e);
}
当我运行这个程序时,我得到以下输出
Listening for connection on port 8332 ....
POST / HTTP/1.1
content-type: application/x-www-form-urlencoded
Content-Length: 37
Host: localhost:8332
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_131)
Accept-Encoding: gzip,deflate
经过调试,程序似乎没有退出这个 while 循环
while ((line = buffer.readLine()) != null) {
System.out.println(line);
request += line;
}
但我不明白为什么。请帮助我卡了一整天。
提前致谢
在 while 循环中使用 !=0 而不是 !=null:
while((line = buffer.readLine()).length() !=0) {
输出:
port 8332 reading: POST / HTTP/1.1content-type: application/x-www-form-urlencodedContent-Length: 18Host: localhost:8332Connection: Keep-AliveUser-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_171)Accept-Encoding: gzip,deflateorg.apache.http.NoHttpResponseException: localhost:8332 failed to respond
But I can't figure out why.
您的服务器从 buffer.readLine()
获得 null
的唯一方法是客户端关闭其套接字输出流。
问题是客户端试图保持连接...这意味着它不会关闭其套接字输出流。这意味着服务器需要尊重“content-length”header;即计算读取的字节数而不是查找 end-of-stream.
从根本上说,您的服务器端没有正确实施 HTTP 1.1 规范。
怎么办?
好吧我的建议是不要尝试从套接字开始实现HTTP。使用现有框架...或 Apache HttpComponents 库。
但如果您坚持这样做1,请在开始尝试实施之前通读 阅读 HTTP 规范。并在您 运行 遇到实施问题时 随时 查阅规范,以检查您是否在做正确的事情。
1 - 定义:受虐狂 - 享受看似痛苦或乏味的 activity。