我应该开发一个程序,用指定的URL读取网页,但问题是我不允许使用任何HTTP库,我只允许使用TCP。Java HTTP GET响应等待,直到超时
下面是读取响应消息中的代码:
private static String readMultiline (BufferedReader inStr) {
String message="";
String line=readLine(inStr);
while (line != null) {
message += line + "\r\n";
line=readLine(inStr);
}
if (message.length() == 0) return null;
return message;
}
private static String readLine (BufferedReader inStr) {
String line = null;
try{
line = inStr.readLine();
} catch (IOException ioe) {
System.out.println("ERROR: Incoming packet could not be read properly.");
return null;
}
return line;
}
的问题是,头部和网页内容被完全接收,但是while循环仍然等待“下一个”线,这不存在。过了一段时间,超时发生,代码继续。我试图连接的服务器不会执行“连接:关闭”。我如何检测文件的结尾?
这是功课吗?为什么你会被禁止使用Java SDK中包含的HTTP库? – Perception 2011-12-15 22:14:06
是的,这是一个家庭作业。尽管如此,就我所见,HTTP库在从流中读取时的工作方式几乎相同。只要它无法检测到文件的结尾,问题就会发生。 – kubuzetto 2011-12-15 22:15:43