对于网络作业,我们需要创建一个HTTP客户端(版本1.1)。问题是
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
writer.println("GET / HTTP/1.1");
writer.println("Host: " + uri.getHost());
writer.println("Accept: text/html");
writer.println();
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null) {
builder.append(line);
}
return builder.toString();
挂起在输入的末尾,因为我们使用http 1.1,我们不能简单地关闭连接。那么如何检测输入的结束?
答案 0 :(得分:1)
那么如何检测输入结束?
遵循标准。 RFC 2616 section 4.4清楚地描述了如何指定消息长度。简而言之,按此顺序: