通过Java的PrintWriter发送HTTP响应

时间:2016-09-09 07:59:30

标签: java sockets http

我正在尝试使用带有PrintWriter的Socket将HTTP响应消息发送到普通的Web浏览器,但是从浏览器的角度来看,它只检测到我发送的HTTP响应头但是无法获得响应体。

String response = "helloworld";
System.out.println("Content is: " + response);

if ((response != null) || (!response.isEmpty)) {
    respCode = "HTTP/1.1 200 OK\r\n"
                + "Date: " + getServerTime() + "\r\n"
                + "Connection: Close \r\n"
                + "Server: MyServer v1.0\r\n"
                + "Content-Length: " + response.length() + "\r\n"
                + "Content-Type: text/html\r\n\r\n"
                + "<html><body>" + response + "</body></html>\r\n";
     return respCode;
} else {
    return null;
}

套接字代码

public void sendHttpResponse(String input) {
    wr.write(input);
    wr.flush();
}

0 个答案:

没有答案