【问题标题】:Simple HTTP Server in C - How to response to Browser?C 中的简单 HTTP 服务器 - 如何响应浏览器?
【发布时间】:2013-03-26 22:57:15
【问题描述】:

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

    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);

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

firefox 插件 HttpRequestHelper 显示:

获取本地主机:6666

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

为什么内容没有加载?

【问题讨论】:

  • 对于 http 标头,使用 \r\n 行终止符。可能不是这样,但我在这里没有看到任何其他错误,并且标准要求协议流量使用 CRLF 终止符

标签: c http networking response


【解决方案1】:

HTTP 要求行以 CR+LF 结尾,所以试试\r\n

【讨论】:

  • @XHotSniperX 这并不意味着你应该滥用标准。
  • 不,这意味着这不应该是一个答案,而是一个评论,因为它不能解决问题。
【解决方案2】:

发送响应后尝试刷新和/或关闭套接字。

【讨论】:

  • @XHotSniperX 在调用 close 之前关闭套接字
猜你喜欢
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-07
  • 2012-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多