深入浅出curl / wget / httpie 用法详解

一、工具简介

1.1 curl

属性说明
首次发布1997 年
原名urlgethttpgetcurl
作者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 三者的关系

互补

竞品

互补

curl
全能 HTTP 调试器

wget
专业下载工具

httpie
人性化交互客户端


二、基础请求

2.1 GET 请求

# ===== curl =====
curl https://httpbin.org/get                    # 最简 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                 # 仅获取响应头(HEAD 请求)

# 带查询参数
curl "https://httpbin.org/get?name=foo&page=1"
curl -G -d "name=foo" -d "page=1" https://httpbin.org/get  # -G 强制 GET

# ===== wget =====
wget -qO- https://httpbin.org/get               # 静默输出到 stdout
wget -d https://httpbin.org/get                  # 调试模式

# ===== httpie =====
http https://httpbin.org/get                    # 默认彩色输出
http -v https://httpbin.org/get                 # 显示完整请求/响应
http -p Hh https://httpbin.org/get              # 仅打印请求头(H)和响应头(h)
http https://httpbin.org/get name==foo page==1  # 直观的查询参数

2.2 POST 请求

# ===== curl =====
# 表单数据
curl -d "username=admin&password=123" https://httpbin.org/post
# JSON 数据
curl -H "Content-Type: application/json" \
     -d '{"name":"foo","age":30}' \
     https://httpbin.org/post
# 从文件读取数据
curl -d @data.json https://httpbin.org/post
# 自动设置 Content-Type
curl --json '{"name":"foo"}' https://httpbin.org/post  # curl 7.82+

# ===== wget =====
wget --post-data="username=admin&password=123" -qO- https://httpbin.org/post

# ===== httpie =====
# 最直观的 JSON POST
http POST https://httpbin.org/post name=foo age:=30 active:=true
# 默认就是 JSON,且自动设置 Content-Type!
# := 表示非字符串类型(数字/布尔/数组)
# 表单提交
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 =====
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

# ===== httpie =====
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 =====
curl -H "Authorization: Bearer token123" \
     -H "X-Custom-Header: value" \
     https://httpbin.org/headers
# 修改 User-Agent
curl -A "MyApp/1.0" https://httpbin.org/headers
# 模拟 Referer
curl -e "https://google.com" https://httpbin.org/headers

# ===== httpie =====
http https://httpbin.org/headers \
     Authorization:"Bearer token123" \
     X-Custom-Header:value \
     User-Agent:"MyApp/1.0"
# 对标 curl,httpie 的 Header 写法更自然

3.2 认证方式

# ===== curl =====
# Basic Auth
curl -u admin:pass123 https://httpbin.org/basic-auth/admin/pass123
# Bearer Token
curl -H "Authorization: Bearer eyJhbG..." https://api.example.com/me
# Digest Auth
curl --digest -u admin:pass123 https://httpbin.org/digest-auth/auth/admin/pass123

# ===== httpie =====
# Basic Auth
http -a admin:pass123 https://httpbin.org/basic-auth/admin/pass123
# Bearer Token(两种写法)
http https://api.example.com/me Authorization:"Bearer eyJhbG..."
http -A bearer -a eyJhbG... https://api.example.com/me   # 简写

四、文件下载

4.1 基本下载

# ===== curl =====
curl -O https://example.com/file.tar.gz          # 使用 URL 中的文件名
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 =====
wget https://example.com/file.tar.gz              # 默认使用 URL 文件名
wget -O myfile.tar.gz https://example.com/file.tar.gz
wget -i urls.txt                                  # 从文件读取 URL 列表批量下载

# ===== httpie =====
http -d https://example.com/file.tar.gz > file.tar.gz   # -d 即 --download

4.2 断点续传与限速

# ===== curl =====
curl -C - -O https://example.com/large.iso       # -C- 自动查找断点
curl --limit-rate 500k -O https://example.com/large.iso

# ===== wget =====
wget -c https://example.com/large.iso             # -c = --continue
wget --limit-rate=500k https://example.com/large.iso

# ===== httpie =====
http -d -c https://example.com/large.iso          # -c 续传

4.3 代理下载

# ===== curl =====
# HTTP 代理
curl -x http://127.0.0.1:10809 -O https://example.com/file.iso
# SOCKS5 代理
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 =====
# HTTP/HTTPS 代理
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
# 注意:wget 不原生支持 SOCKS5,需借助 tsocks/proxychains

# ===== httpie =====
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
# 格式:--proxy=<目标协议>:<代理协议>://<代理地址>:<端口>
# SOCKS5 需要 pip install pysocks

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(最常用)
curl ip.sb
curl ifconfig.me
curl icanhazip.com
curl -4 ip.sb        # 仅 IPv4
curl -6 ip.sb        # 仅 IPv6

# 修改 User-Agent(部分服务会限制非浏览器 UA)
curl -A "Mozilla/5.0" ip.sb

# httpie
http ip.sb
http -b ip.sb          # 仅输出 body

# wget(不擅长此场景,但也可以)
wget -qO- ip.sb

5.2 查看完整请求/响应

# ===== 查看详细通信过程 =====
curl -v https://httpbin.org/get          # 显示请求头、响应头、TLS 握手
curl --trace-ascii trace.txt https://httpbin.org/get  # 完整转储到文件

# ===== 仅查看响应头 =====
curl -I https://httpbin.org/get           # HEAD 请求
curl -s -o /dev/null -D - https://httpbin.org/get  # 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_namelookupDNS 解析耗时
time_connectTCP 三次握手耗时
time_appconnectTLS/SSL 握手耗时
time_starttransfer首字节时间(TTFB)
time_total总耗时

5.3 跟随重定向

# ===== curl =====
curl -L https://bit.ly/xxxx              # -L 跟随重定向
curl -L --max-redirs 5 https://bit.ly/xxxx # 限制最大重定向次数

# ===== wget =====
wget --max-redirect=5 https://bit.ly/xxxx

# ===== httpie =====
http --follow https://bit.ly/xxxx         # 默认跟随 30 次

5.4 自定义 DNS 解析

# curl:绕过 DNS,直接指定 IP(调试虚拟主机、CDN 切换)
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 =====
curl -F "file=@photo.jpg" https://httpbin.org/post
curl -F "file=@photo.jpg;type=image/jpeg" https://httpbin.org/post  # 指定 MIME
curl -F "file1=@a.jpg" -F "file2=@b.png" https://httpbin.org/post   # 多文件

# ===== httpie =====
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 保留原始字节(不处理换行符)
curl --data-binary @image.png https://httpbin.org/post

七、Cookie 与 Session

# ===== curl =====
# 发送 Cookie
curl -b "session=abc123" https://httpbin.org/cookies
# 从文件读取 Cookie(Netscape 格式)
curl -b cookies.txt https://httpbin.org/cookies
# 保存 Cookie 到文件
curl -c cookies.txt https://httpbin.org/cookies/set?k=v
# 同时读写 Cookie 文件(-b 和 -c 可用同一文件)
curl -b jar.txt -c jar.txt https://example.com/login

# ===== httpie =====
http https://httpbin.org/cookies "Cookie:session=abc123"
# 启用 session 持久化
http --session=mysession POST https://example.com/login user=admin
http --session=mysession GET https://example.com/dashboard

八、HTTPS 与 SSL/TLS

# ===== curl =====
# 忽略证书验证(仅测试用!)
curl -k https://self-signed.badssl.com/
# 指定 CA 证书
curl --cacert ca.pem https://example.com/
# 指定客户端证书(双向 TLS)
curl --cert client.pem --key client-key.pem https://example.com/
# 指定 TLS 版本
curl --tlsv1.2 https://example.com/
# 查看 TLS 握手信息
curl -v https://example.com/ 2>&1 | grep -E "SSL|TLS|subject|issuer"

# ===== httpie =====
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)

# 并发 10 个请求
curl -Z https://httpbin.org/delay/1 -o /dev/null

# 单 URL 重复请求(利用 brace expansion)
for i in {1..100}; do
    curl -s -o /dev/null -w "%{http_code} %{time_total}\n" https://api.example.com/ &
done
wait

# 更专业的方式:用 xargs 并发
seq 1 100 | xargs -P10 -I{} curl -s -o /dev/null -w "%{time_total}\n" https://httpbin.org/get

curl 不是专业压测工具,正式的基准测试建议使用 abwrkheyk6

9.2 网速测试(下载大文件)

# 测试下载速度
curl -o /dev/null -w "Speed: %{speed_download} bytes/sec\n" \
     https://speedtest.example.com/100MB.bin

# wget 方式
wget -O /dev/null https://speedtest.example.com/100MB.bin

十、脚本与自动化

10.1 curl 在 Shell 脚本中

#!/bin/bash
# 静默 API 调用,解析 JSON 响应

# 方式1:用 jq 解析(推荐)
response=$(curl -s https://api.github.com/repos/curl/curl)
stars=$(echo "$response" | jq -r '.stargazers_count')
echo "curl has $stars stars"

# 方式2:仅获取 HTTP 状态码
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

# 方式3:带重试的健壮请求
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
# -b: 后台运行
# -o: 日志输出到文件

# 查看进度
tail -f download.log

10.3 httpie 作为 Python 替代

# httpie 输出天然适合阅读,但脚本中建议:
http -b https://api.example.com/users/1 | jq '.email'
# -b 仅输出 body(去掉头信息和颜色)

十一、常用命令

场景curlwgethttpie
GET 请求curl URLwget -qO- URLhttp URL
POST JSONcurl -d '{...}' -H 'Content-Type: application/json' URL不擅长http POST URL key=val
下载文件curl -O URLwget URLhttp -d URL
断点续传curl -C - -O URLwget -c URLhttp -d -c URL
显示响应头curl -I URLwget -S --spider URLhttp -h URL
跟随重定向curl -L URLwget URL(默认跟随)http --follow URL
整站镜像不支持wget -mkEpnp URL不支持
Basic Authcurl -u user:pass URLwget --user=user --password=pass URLhttp -a user:pass URL
Bearer Tokencurl -H 'Authorization: Bearer TOKEN' URLwget --header='...' URLhttp URL Authorization:'Bearer TOKEN'
自定义 Headercurl -H 'X-Key: val' URLwget --header='X-Key: val' URLhttp URL X-Key:val
HTTP 代理curl -x http://proxy:port URLwget -e 'http_proxy=...' URLhttp URL --proxy=http:http://proxy:port
SOCKS5 代理curl -x socks5://... URL需借助外部工具http URL --proxy=https:socks5://...
JSON 彩色输出(需管道 jq)默认
静默模式curl -s URLwget -q URLhttp -q URL
耗时分析curl -w '@fmt.txt' URL不支持不支持
忽略 SSL 错误curl -k URLwget --no-check-certificate URLhttp --verify=no URL

十二、备注

场景推荐工具理由
API 调试与开发httpie彩色输出 + JSON 友好 + 语法直观
Shell 脚本自动化curl原生内置、参数完备、-w 耗时分析
大文件下载wget断点续传最可靠、后台下载
整站离线镜像wget唯一选择,功能完备
Docker / 最小化环境curl / wgethttpie 需 Python 环境
HTTPS 双向认证调试curl证书参数最灵活、TLS 版本控制
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值