从套接字读取 http post 内容时出现问题,仅获取标头
服务器
public void HandleConnection(Socket socket) {
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
try {
while ((strLine = br.readLine()) != null) {
System.out.println (strLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
客户端
public void httpPostTest() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.2:8080/");
try {
httppost.setEntity(new StringEntity("test"));
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
对此有什么想法吗?这就是我返回的全部内容,只是标题:
POST / HTTP/1.1
Content-Length: 4
Content-Type: text/plain; charset=ISO-8859-1
Host: 192.168.0.2:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.2 (java 1.5)
public String readLine(InputStream inputStream) {
StringWriter writer = new StringWriter();
try {
IOUtils.copy(inputStream, writer, "ASCII");
} catch (IOException e) {
e.printStackTrace();
}
String theString = writer.toString();
return theString.trim();
}
server
public void HandleConnection(Socket socket) {
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
try {
while ((strLine = br.readLine()) != null) {
System.out.println (strLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
client
public void httpPostTest() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.2:8080/");
try {
httppost.setEntity(new StringEntity("test"));
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Any thoughts on this? This is all I get back, just the header:
POST / HTTP/1.1
Content-Length: 4
Content-Type: text/plain; charset=ISO-8859-1
Host: 192.168.0.2:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.2 (java 1.5)
public String readLine(InputStream inputStream) {
StringWriter writer = new StringWriter();
try {
IOUtils.copy(inputStream, writer, "ASCII");
} catch (IOException e) {
e.printStackTrace();
}
String theString = writer.toString();
return theString.trim();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我阅读后的理解,BufferedReader 是从 http post 读取 inputStreams 的糟糕方法。我所经历的是,发出帖子的客户端会挂起,因此每个帖子上的连接处理程序线程是什么,当我杀死客户端时,服务器将停止挂起,我会看到消息正文。根据我在互联网上收集的信息,读取输入流的最佳方法是逐字节地遍历它,并在字节总和==内容长度值后退出循环。
也就是说,我计划使用 Apache HTTPCore 来处理这个问题,因为这似乎是一个更好的解决方案
From what I understand after doing some reading, BufferedReader is a bad way to read inputStreams from http post. What I was experiencing was that the client making the post would hang and so what the connection handler thread on every post, when I would kill the client the server would stop hanging and I would see the message body. From what I gathered on the internet, the best way to read the input stream is to step through it byte by byte, and exit the loop after the sum of the bytes == the content-length value.
That said, I'm planning on using Apache HTTPCore instead to handle this as that seems like a better solution
如果有人像我一样来到这里,这就是我写的
For whats worth, if someone comes here like me, here's what I wrote