为什么服务器撤回的HTML页面未在浏览器中呈现

时间:2019-03-20 06:35:24

标签: html c++ httpserver

我已经创建了一个简单的HTTP服务器(当前仅支持GET),该服务器每当发出GET请求时都会返回一个HTML页面,但是返回的HTML页面不会在浏览器中呈现,我验证了我的服务器确实在使用CURL和CURL能够获取数据。该代码可以在下面找到

GITHUB_LINK

下面是curl的输出,您可以看到服务器正在返回HTML页面。

$ curl -G "192.168.1.188:8080" --verbose
* Rebuilt URL to: 192.168.1.188:8080/
* Hostname was NOT found in DNS cache
*   Trying 192.168.1.188...
* Connected to 192.168.1.188 (192.168.1.188) port 8080 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 192.168.1.188:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
<  Content-type: text/html
<  
< <!DOCTYPE html> 
<  <html>
<  <head> 
<  <title>simple server</title> 
<  </head> 
<   <body>
<   <h1>Welcome</h1>
<  <p>Welcome to the server .</p>
<   </body>
<  </html>
* Connection #0 to host 192.168.1.188 left intact

1 个答案:

答案 0 :(得分:2)

在发送HTML页面之前,您需要向浏览器发送一些标题。仅发送文件内容是不够的。检查文档https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages#HTTP_Responses

上回复的结构

尝试从工作的Web服务器访问页面,在浏览器中查找标题结构(使用浏览器的开发工具),并使用您的代码进行复制。