一、工具简介
1.1 curl
| 属性 | 说明 |
|---|
| 首次发布 | 1997 年 |
| 原名 | urlget → httpget → curl |
| 作者 | Daniel Stenberg(最初为 IRC 用户自动获取汇率) |
| 核心定位 | 模拟 HTTP 请求,与 Web API 交互 |
| 协议支持 | HTTP/HTTPS、FTP、SFTP、LDAP、SMTP、MQTT 等 20+ 种 |
curl 的特点:极其灵活的参数组合,几乎可以构造任意 HTTP 请求,是 API 调试和自动化脚本的首选。
1.2 wget
| 属性 | 说明 |
|---|
| 首次发布 | 1995 年年底 |
| 原名 | geturl |
| 名称含义 | World Wide Web + Get |
| 核心定位 | 文件下载、站点镜像 |
| 协议支持 | HTTP/HTTPS、FTP |
wget 的特点:专注于下载,支持递归抓取、断点续传、后台静默下载,适合批量下载和站点备份。
1.3 httpie
| 属性 | 说明 |
|---|
| 发音 | aitch-tee-tee-pie |
| 语言 | Python(基于 Requests + Pygments) |
| 核心定位 | 命令行交互式 HTTP 调试 |
| 特色 | 语法高亮、JSON 友好、直观的请求语法 |
httpie 的特点:对人类友好的输出格式——彩色 JSON、自动格式化、直观的参数写法,交互体验远超 curl。
1.4 三者的关系
二、基础请求
2.1 GET 请求
curl https://httpbin.org/get
curl -v https://httpbin.org/get
curl -s https://httpbin.org/get
curl -i https://httpbin.org/get
curl -I https://httpbin.org/get
curl "https://httpbin.org/get?name=foo&page=1"
curl -G -d "name=foo" -d "page=1" https://httpbin.org/get
wget -qO- https://httpbin.org/get
wget -d https://httpbin.org/get
http https://httpbin.org/get
http -v https://httpbin.org/get
http -p Hh https://httpbin.org/get
http https://httpbin.org/get name==foo page==1
2.2 POST 请求
curl -d "username=admin&password=123" https://httpbin.org/post
curl -H "Content-Type: application/json" \
-d '{"name":"foo","age":30}' \
https://httpbin.org/post
curl -d @data.json https://httpbin.org/post
curl --json '{"name":"foo"}' https://httpbin.org/post
wget --post-data="username=admin&password=123" -qO- https://httpbin.org/post
http POST https://httpbin.org/post name=foo age:=30 active:=true
http -f POST https://httpbin.org/post username=admin password=123
http POST https://httpbin.org/post < data.json
2.3 PUT / PATCH / DELETE
curl -X PUT -d '{"name":"bar"}' https://httpbin.org/put
curl -X PATCH -d '{"age":31}' https://httpbin.org/patch
curl -X DELETE https://httpbin.org/delete
http PUT https://httpbin.org/put name=bar
http PATCH https://httpbin.org/patch age:=31
http DELETE https://httpbin.org/delete
三、请求头与认证
3.1 自定义请求头
curl -H "Authorization: Bearer token123" \
-H "X-Custom-Header: value" \
https://httpbin.org/headers
curl -A "MyApp/1.0" https://httpbin.org/headers
curl -e "https://google.com" https://httpbin.org/headers
http https://httpbin.org/headers \
Authorization:"Bearer token123" \
X-Custom-Header:value \
User-Agent:"MyApp/1.0"
3.2 认证方式
curl -u admin:pass123 https://httpbin.org/basic-auth/admin/pass123
curl -H "Authorization: Bearer eyJhbG..." https://api.example.com/me
curl --digest -u admin:pass123 https://httpbin.org/digest-auth/auth/admin/pass123
http -a admin:pass123 https://httpbin.org/basic-auth/admin/pass123
http https://api.example.com/me Authorization:"Bearer eyJhbG..."
http -A bearer -a eyJhbG... https://api.example.com/me
四、文件下载
4.1 基本下载
curl -O https://example.com/file.tar.gz
curl -o myfile.tar.gz https://example.com/file.tar.gz
curl -O https://example.com/file{1,2,3}.tar.gz
curl -O https://example.com/file[1-10].txt
wget https://example.com/file.tar.gz
wget -O myfile.tar.gz https://example.com/file.tar.gz
wget -i urls.txt
http -d https://example.com/file.tar.gz > file.tar.gz
4.2 断点续传与限速
curl -C - -O https://example.com/large.iso
curl --limit-rate 500k -O https://example.com/large.iso
wget -c https://example.com/large.iso
wget --limit-rate=500k https://example.com/large.iso
http -d -c https://example.com/large.iso
4.3 代理下载
curl -x http://127.0.0.1:10809 -O https://example.com/file.iso
curl -x socks5://127.0.0.1:10808 -O https://example.com/file.iso
curl --socks5 127.0.0.1:10808 -O https://example.com/file.iso
wget -e "http_proxy=http://127.0.0.1:10809" https://example.com/file.iso
wget -e "https_proxy=http://127.0.0.1:10809" https://example.com/file.iso
http -d https://example.com/file.iso --proxy=https:http://127.0.0.1:10809
http -d https://example.com/file.iso --proxy=https:socks5://127.0.0.1:10808
4.4 wget 独有:整站镜像
wget 最强独有功能——递归下载整个网站,生成离线浏览副本:
wget --mirror \
--convert-links \
--adjust-extension \
--page-requisites \
--no-parent \
http://example.org \
-P ./html
wget -mkEpnp http://example.org -P ./html
| 参数 | 含义 |
|---|
--mirror / -m | 启用镜像模式(递归 + 无限深度 + 时间戳) |
--convert-links / -k | 将链接转为相对路径,适合离线浏览 |
--adjust-extension / -E | 根据 Content-Type 自动添加 .html/.css 后缀 |
--page-requisites / -p | 下载页面所需的 CSS/JS/图片等资源 |
--no-parent / -np | 不递归到父目录,限制只下载子路径 |
-P | 指定本地保存目录 |
五、调试与排查
5.1 获取本机外网 IP
curl ip.sb
curl ifconfig.me
curl icanhazip.com
curl -4 ip.sb
curl -6 ip.sb
curl -A "Mozilla/5.0" ip.sb
http ip.sb
http -b ip.sb
wget -qO- ip.sb
5.2 查看完整请求/响应
curl -v https://httpbin.org/get
curl --trace-ascii trace.txt https://httpbin.org/get
curl -I https://httpbin.org/get
curl -s -o /dev/null -D - https://httpbin.org/get
curl -w "\n time_namelookup: %{time_namelookup}\n \
time_connect: %{time_connect}\n \
time_appconnect: %{time_appconnect}\n \
time_pretransfer: %{time_pretransfer}\n \
time_redirect: %{time_redirect}\n \
time_starttransfer: %{time_starttransfer}\n \
time_total: %{time_total}\n" \
-o /dev/null -s https://httpbin.org/get
curl -w 变量速查:
| 变量 | 含义 |
|---|
time_namelookup | DNS 解析耗时 |
time_connect | TCP 三次握手耗时 |
time_appconnect | TLS/SSL 握手耗时 |
time_starttransfer | 首字节时间(TTFB) |
time_total | 总耗时 |
5.3 跟随重定向
curl -L https://bit.ly/xxxx
curl -L --max-redirs 5 https://bit.ly/xxxx
wget --max-redirect=5 https://bit.ly/xxxx
http --follow https://bit.ly/xxxx
5.4 自定义 DNS 解析
curl --resolve example.com:443:1.2.3.4 https://example.com/
curl --resolve example.com:80:192.168.1.1 http://example.com/
六、上传文件
6.1 表单文件上传
curl -F "file=@photo.jpg" https://httpbin.org/post
curl -F "file=@photo.jpg;type=image/jpeg" https://httpbin.org/post
curl -F "file1=@a.jpg" -F "file2=@b.png" https://httpbin.org/post
http -f POST https://httpbin.org/post file@photo.jpg
http -f POST https://httpbin.org/post file1@a.jpg file2@b.png
6.2 直接上传二进制 Body
curl --data-binary @image.png https://httpbin.org/post
七、Cookie 与 Session
curl -b "session=abc123" https://httpbin.org/cookies
curl -b cookies.txt https://httpbin.org/cookies
curl -c cookies.txt https://httpbin.org/cookies/set?k=v
curl -b jar.txt -c jar.txt https://example.com/login
http https://httpbin.org/cookies "Cookie:session=abc123"
http --session=mysession POST https://example.com/login user=admin
http --session=mysession GET https://example.com/dashboard
八、HTTPS 与 SSL/TLS
curl -k https://self-signed.badssl.com/
curl --cacert ca.pem https://example.com/
curl --cert client.pem --key client-key.pem https://example.com/
curl --tlsv1.2 https://example.com/
curl -v https://example.com/ 2>&1 | grep -E "SSL|TLS|subject|issuer"
http --verify=no https://self-signed.badssl.com/
http --verify=ca.pem https://example.com/
http --cert=client.pem --cert-key=client-key.pem https://example.com/
九、性能测试
9.1 并发压测(curl)
curl -Z https://httpbin.org/delay/1 -o /dev/null
for i in {1..100}; do
curl -s -o /dev/null -w "%{http_code} %{time_total}\n" https://api.example.com/ &
done
wait
seq 1 100 | xargs -P10 -I{} curl -s -o /dev/null -w "%{time_total}\n" https://httpbin.org/get
curl 不是专业压测工具,正式的基准测试建议使用 ab、wrk、hey 或 k6。
9.2 网速测试(下载大文件)
curl -o /dev/null -w "Speed: %{speed_download} bytes/sec\n" \
https://speedtest.example.com/100MB.bin
wget -O /dev/null https://speedtest.example.com/100MB.bin
十、脚本与自动化
10.1 curl 在 Shell 脚本中
#!/bin/bash
response=$(curl -s https://api.github.com/repos/curl/curl)
stars=$(echo "$response" | jq -r '.stargazers_count')
echo "curl has $stars stars"
http_code=$(curl -s -o /dev/null -w "%{http_code}" https://httpbin.org/status/200)
if [ "$http_code" = "200" ]; then
echo "Service is healthy"
else
echo "Service returned $http_code"
fi
curl --retry 3 --retry-delay 5 --max-time 30 \
https://api.example.com/data
10.2 wget 后台静默下载
wget -b -o download.log https://example.com/hugefile.iso
tail -f download.log
10.3 httpie 作为 Python 替代
http -b https://api.example.com/users/1 | jq '.email'
十一、常用命令
| 场景 | curl | wget | httpie |
|---|
| GET 请求 | curl URL | wget -qO- URL | http URL |
| POST JSON | curl -d '{...}' -H 'Content-Type: application/json' URL | 不擅长 | http POST URL key=val |
| 下载文件 | curl -O URL | wget URL | http -d URL |
| 断点续传 | curl -C - -O URL | wget -c URL | http -d -c URL |
| 显示响应头 | curl -I URL | wget -S --spider URL | http -h URL |
| 跟随重定向 | curl -L URL | wget URL(默认跟随) | http --follow URL |
| 整站镜像 | 不支持 | wget -mkEpnp URL | 不支持 |
| Basic Auth | curl -u user:pass URL | wget --user=user --password=pass URL | http -a user:pass URL |
| Bearer Token | curl -H 'Authorization: Bearer TOKEN' URL | wget --header='...' URL | http URL Authorization:'Bearer TOKEN' |
| 自定义 Header | curl -H 'X-Key: val' URL | wget --header='X-Key: val' URL | http URL X-Key:val |
| HTTP 代理 | curl -x http://proxy:port URL | wget -e 'http_proxy=...' URL | http URL --proxy=http:http://proxy:port |
| SOCKS5 代理 | curl -x socks5://... URL | 需借助外部工具 | http URL --proxy=https:socks5://... |
| JSON 彩色输出 | (需管道 jq) | – | 默认 |
| 静默模式 | curl -s URL | wget -q URL | http -q URL |
| 耗时分析 | curl -w '@fmt.txt' URL | 不支持 | 不支持 |
| 忽略 SSL 错误 | curl -k URL | wget --no-check-certificate URL | http --verify=no URL |
十二、备注
| 场景 | 推荐工具 | 理由 |
|---|
| API 调试与开发 | httpie | 彩色输出 + JSON 友好 + 语法直观 |
| Shell 脚本自动化 | curl | 原生内置、参数完备、-w 耗时分析 |
| 大文件下载 | wget | 断点续传最可靠、后台下载 |
| 整站离线镜像 | wget | 唯一选择,功能完备 |
| Docker / 最小化环境 | curl / wget | httpie 需 Python 环境 |
| HTTPS 双向认证调试 | curl | 证书参数最灵活、TLS 版本控制 |