【发布时间】:2012-08-16 04:29:24
【问题描述】:
似乎使用 curl 和大多数网络浏览器,我的服务器代码在客户端能够读取响应之前关闭连接。这是我的代码
public void run() {
try {
InputStream input = clientSocket.getInputStream();
OutputStream output = clientSocket.getOutputStream();
System.out.println(input);
// getRequestObject(input);
long time = System.currentTimeMillis();
output.write(("HTTP/1.1 200 OK\n\nWorkerRunnable: " + this.serverText + " - " + time + "").getBytes());
output.flush();
output.close();
input.close();
System.out.println("Request processed: " + time);
} catch (IOException e) {
// report exception somewhere.
e.printStackTrace();
}
}
protected String readInputStream(InputStream input) throws IOException {
String inputLine;
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuilder sb = new StringBuilder();
while (!(inputLine = in.readLine()).equals("")) {
sb.append(inputLine);
}
return sb.toString();
}
有什么想法吗?
【问题讨论】:
-
不知道是否相关,但 http 标头以
\r\n结尾而不是\n -
正如@rambo 已经提到的,查看维基百科的介绍:en.wikipedia.org/wiki/…
-
在关闭套接字之前,应发出关闭并等待确认。我的套接字编程都是用 C++ 编写的,所以我不确定如何用 Java 解决它,但下面的文章可能会帮助您了解问题blog.netherlabs.nl/articles/2009/01/18/…