从套接字读取 http post 内容时出现问题,仅获取标头

发布于 2024-12-01 01:32:07 字数 1597 浏览 2 评论 0原文

服务器

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最佳男配角 2024-12-08 01:32:07

根据我阅读后的理解,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

拥有 2024-12-08 01:32:07

如果有人像我一样来到这里,这就是我写的

InputStream stream = socket.getInputStream();

byte[] b = new byte[1024];
while(stream.read(b) > 0 && !socket.isInputShutdown()) {
    System.out.println(new String(b));
    b = null;
    b = new byte[1024];
}

For whats worth, if someone comes here like me, here's what I wrote

InputStream stream = socket.getInputStream();

byte[] b = new byte[1024];
while(stream.read(b) > 0 && !socket.isInputShutdown()) {
    System.out.println(new String(b));
    b = null;
    b = new byte[1024];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文