【发布时间】:2010-06-08 15:29:03
【问题描述】:
我有一个 DataInputStream,它是从 Java 中的 Socket 创建的。连接到一个使用分块传输编码的简单 Web 服务器。 Web 服务器实际上可以在普通浏览器中工作。但是在我的程序中,我试图读取,我读取了第一个字节(大约 5kb 的数据)。但是之后的每次读取都会返回读取的 0 个字节。它不应该阻塞直到它可以读取吗?
注意:这通常不会发生。问题出在我要连接的服务器上。
此外,即使在 bytesread == 0 之后,这里的代码也都返回 false。:
System.out.println(socket.isClosed());
System.out.println(socket.isInputShutdown());
System.out.println(socket.isOutputShutdown());
这里是相应的标题:
HTTP/1.1 200 OK
Date: Tue, 08 Jun 2010 14:01:01 GMT
Server: Apache/2.2.11 (Unix) PHP/5.2.10
X-Powered-By: PHP/5.2.10
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
【问题讨论】:
-
我们是否应该假设您的意思是
ByteArrayInputStream而不是ByteInputStream? -
完全抱歉。其实就是DataInputStream!
-
这很奇怪...
DataInputStream的读取方法应该阻塞。我问的原因是因为ByteArrayInputStream似乎是阻塞读取规则的例外(它从不阻塞)。
标签: java http network-protocols