HTTP报文遵循ABNF(一种形式化的语法表示方式),空格或回车换行都有严格的规定,以此保证HTTP协议的实现
1、最简单的HTTP请求报文
- 请求行:方法
空格
URI空格
HTTP/*.*回车换行
- 请求头(可选,以键值对的形式)
- 空行(回车换行,这一个空行是有实际意义的,代表头与体的分隔,必须要有):
回车换行
- 请求体(可选)
GET /hello/world HTTP/1.1
Host: localhost:8080
2、最简单的HTTP响应报文
- 响应行:HTTP/*.*
空格
状态码空格
状态描述(可选)回车换行
- 响应头(可选,以键值对的形式)
- 空行(必需):
回车换行
- 响应体(可选,任意数据)
HTTP/1.1 200
Content-Length: 11
Hello World
3、使用XShell进行终端模拟HTTP请求
[C:\~]$ telnet localhost 8080
Host 'localhost' resolved to ::1.
Connecting to ::1:8080...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
GET /hello/world HTTP/1.1
Host:localhost:8080
HTTP/1.1 200
Content-Length: 11
Date: Sat, 02 Jan 2021 01:47:23 GMT
Hello WorldConnection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(localhost:8080) at 09:47:44.
Type `help' to learn how to use Xshell prompt.
[C:\~]$