个人经验看网络的学习可能比较需要多动手观察和体会,因为协议很多,概念也比较繁杂,不可能每个细节都了然于胸。
Axios 本身只是对 XHR 和 fetch API、还有 Node http 模块的封装,确实比较局限于应用层和 HTTP 这块了。
在写的过程中,可以试试用 WireShark 抓包玩一玩,观察一下每个数据包在不同层包含的信息。
还可以用 curl, nc, telnet 之类的 unix 工具去尝试模拟一些请求并观察,比如说用 curl -vvv
可以看到一个请求的发送和接收过程:
$ curl -vvv https://baidu.com
* Rebuilt URL to: https://baidu.com/
* Trying 39.156.69.79...
* TCP_NODELAY set
* Connected to baidu.com (39.156.69.79) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: C=CN; ST=\U5317\U4EAC\U5E02; O=BeiJing Baidu Netcom Science Technology Co., Ltd; CN=www.baidu.cn
* start date: Jan 25 00:00:00 2021 GMT
* expire date: Feb 24 23:59:59 2022 GMT
* subjectAltName: host "baidu.com" matched cert's "baidu.com"
* issuer: C=US; O=DigiCert Inc; CN=DigiCert Secure Site Pro CN CA G3
* SSL certificate verify ok.
> GET / HTTP/1.1
> Host: baidu.com
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Server: bfe/1.0.8.18
< Date: Mon, 24 May 2021 06:27:34 GMT
< Content-Type: text/html
< Content-Length: 161
< Connection: keep-alive
< Location: http://www.baidu.com/
<
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
另外还可以玩玩 Python 或者 Node.js 针对操作系统的 Socket API 的封装,通过高级语言的方式,抓住一些关键概念。