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

问题描述 投票:0回答:1

我创建了一个简单的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
html c++ httpserver
1个回答
2
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.