您缺少Content-Length
标题。Connection: close
如果您包含作为标头以指示连接的关闭将表示内容的结束,则这不是必需的。
const char msg[] =
"HTTP/1.1 403 Forbidden\r\n"
"Connection: close\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"<!DOCTYPE html>\r\n"
"<html>\r\n"
"\t<head>\r\n"
"\t\t<title>403 - Forbidden</title>\r\n"
"\t</head>\r\n"
"\t<body>\r\n"
"\t\t<h1>403 - Forbidden</h1>\r\n"
"\t</body>\r\n</html>"
;
char sendBuffer[256];
snprintf(sendBuffer, sizeof(sendBuffer), "%s", msg);
否则,您将不得不动态或静态地将消息正文长度插入到Content-Length
标头中。
const char msg_header[] =
"HTTP/1.1 403 Forbidden\r\n"
"Content-Length: %d\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"%s"
;
const char msg_body[] =
"<!DOCTYPE html>\r\n"
"<html>\r\n"
"\t<head>\r\n"
"\t\t<title>403 - Forbidden</title>\r\n"
"\t</head>\r\n"
"\t<body>\r\n"
"\t\t<h1>403 - Forbidden</h1>\r\n"
"\t</body>\r\n</html>"
;
char sendBuffer[256];
snprintf(sendBuffer, sizeof(sendBuffer), msg_header,
sizeof(msg_body)-1, msg_body);
Connection: close
正如 Eddy_Em 所指出的,如果服务器实际上关闭了连接,则无需声明。如果您的服务器在发出此消息后没有关闭连接,则需要内容长度。