在 Linux 系统中,你可以使用 telnet 命令来测试 HTTP 服务。以下是使用 telnet 测试 HTTP 服务的基本步骤:
打开终端。
输入以下命令,将 your_http_server 替换为你要测试的 HTTP 服务器的域名或 IP 地址,将 your_port 替换为 HTTP 服务器的端口号(通常为 80 或 443):
telnet your_http_server your_port
例如,要测试访问 www.example.com 的 HTTP 服务,你可以输入:
telnet www.example.com 80
Trying xxx.xxx.xxx.xxx...
Connected to www.example.com.
Escape character is '^]'.
现在你已经连接到了 HTTP 服务器,可以输入 HTTP 请求。例如,要请求访问网站的首页,你可以输入以下内容:
GET / HTTP/1.1
Host: www.example.com
Connection: close
注意,在请求的末尾有两个换行符(\r\n),它们表示请求头的结束。
HTTP/1.1 200 OK
Date: Mon, 23 May 2022 22:38:34 GMT
Server: Apache/2.4.1 (Unix)
Last-Modified: Wed, 08 Jan 2022 23:11:55 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 138
Connection: close
<html>
<head>
<title>An Example Page</title>
</head>
<body>
Hello World, this is a very simple HTML document.
</body>
</html>
quit 命令并按 Enter 键来关闭连接。请注意,telnet 不是一个安全的工具,因为它不使用加密。在测试 HTTPS 服务时,建议使用 openssl 命令。