C中的简单HTTP服务器 - 如何响应浏览器?

XHo*_*erX 4 c networking http response

我的服务器正在向请求发送带有正文的示例响应标头:

    static char* not_found_response_template = 
      "HTTP/1.1 404 Not Found\n"
      "Content-type: text/html\n"
      "\n"
      "<html>\n"
      " <body>\n"
      "  <h1>Not Found</h1>\n"
      "  <p>The requested URL was not found on this server.</p>\n"
      " </body>\n"
      "</html>\n";


    len = strlen(not_found_response_template);
    send(newSct, not_found_response_template, len, 0);
Run Code Online (Sandbox Code Playgroud)

它正确发送它,但firefox继续加载,直到我取消传输.

firefox插件HttpRequestHelper显示了这个:

获取localhost:6666

- 响应 - 404未找到内容类型:text/html

为什么内容没有加载?

Ker*_* SB 5

HTTP要求CR + LF终止行,所以试试\r\n.

  • @XHotSniperX这并不意味着你应该滥用标准. (4认同)