gpt4 book ai didi

html - 为什么服务器返回的 HTML 页面没有在浏览器中呈现

转载 作者:行者123 更新时间:2023-11-28 01:22:57 27 4
gpt4 key购买 nike

我已经创建了一个简单的 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 页面之前,您需要向浏览器发送一些 header 。仅发送文件的内容是不够的。检查文档中响应的结构 https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages#HTTP_Responses

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

关于html - 为什么服务器返回的 HTML 页面没有在浏览器中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55254811/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com