in = new BufferedReader (new InputStreamReader(client.getInputStream()));
out = new DataOutputStream(client.getOutputStream());
ps = new PrintStream(out);
public void run() {
String line;
try {
while ((line = in.readLine()) != null && line.length()>0) {
System.out.println("got header line: " + line);
}
ps.println("HTTP/1.0 200 OK");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ps.println("Content-type: text/html\n\n");
ps.println("<HTML> <HEAD>hello</HEAD> </HTML>");
}程序运行时没有错误,并且ps.println不会在浏览器中打印任何内容。知道为什么吗?
发布于 2012-01-10 00:21:54
你有没有试过冲洗这条小溪?在没有任何其他信息的情况下,我猜你的PrintStream正在存储字符,但实际上并没有输出它们(为了提高效率)。
有关详细信息,请参阅flush()。
发布于 2012-01-10 00:43:03
你有几个问题。第一:根据HTTP标准:
请求行和标题都必须以结尾(即,回车后跟换行符)。
因此,您需要发送"\r\n“字符来结束行。
此外,您正在使用带有"\n“字符的println函数。Println还会在字符串末尾添加换行符。
因此,您需要更改以下行:
ps.println("HTTP/1.0 200 OK");
...
ps.println("Content-type: text/html\n\n");至
ps.print("HTTP/1.0 200 OK\r\n")
ps.print("Content-type: text/html\r\n\r\n");另外,别忘了用flush();
https://stackoverflow.com/questions/8791574
复制相似问题