首页 > 解决方案 > Java,我无法在控制台上打印整个响应

问题描述

我无法在控制台上打印来自服务器的整个响应!,有3种方法可以绕过这件事,

  1. 添加此标题连接:关闭
  2. HTTP/1.1替换为HTTP/1.0
  3. 添加这个s.close(); // Socket.close();

我无法关闭连接,因为我想在同一个连接上发送多次,

我只想打印整个响应而不关闭连接。

这是我的代码:

            String Ch2 = "";


            sops.write(my_UTF8Byte);
            

            while((Ch2 = reader.readLine()) != null){
                System.out.println(Ch2);
                //sops.write(my_UTF8Byte);
                Thread.sleep(10);
                
            }
            
        //s.close(); This will solve the problem

标签: javasockets

解决方案


当您说不打印整个响应时,它缺少最后一行?通过一次执行一个字符而不是 readLine 来检查:

int ch;
while((ch = in.read()) != -1)
{
    System.out.print((char)ch);
}

推荐阅读