前段时间有同学反馈,不能正常访问开放平台的测试环境,使用 curl 命令,发现了端倪,于是整理一下 curl 的一些基础用法。
案例入门
shell
uzong@MacBook-Air ~ % curl -v -X POST \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Ca-Timestamp: 1743126273976" \
-H "X-Ca-Nonce: 2c6ad4ec-c255-4098-9411-09d442e7ef8c" \
-H "X-Site-Tenant: US_AMZ" \
-H "Content-MD5: 0y4yBh50w0H73OZQih7cAQ==" \
-H "X-Ca-Key: 204840648" \
-H "X-Ca-Signature: cvtVD6BnadR3xtZNVpCZSZP/GN6QvlHOoxxADA0uT9k=" \
-H "X-Ca-Signature-Method: HmacSHA256" \
-d '{"current_page":1,"page_size":1,"ssku_list":["01259-3020-0020","01259-3110-0010"]}' \
https://xxx-open-tst.xxxio.com/api/v1/page_product
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 121.40.69.55:443...
* Connected to xxx-open-tst.xxxio.com (121.40.69.55) port 443 (#0)
* ALPN: offers h2,http/1.1
* (304) (OUT), TLS handshake, Client hello (1):
* CAfile: /etc/ssl/cert.pem
* CApath: none
* (304) (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, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN: server accepted h2
* Server certificate:
* subject: C=CN; ST=ZheJiang; L=HangZhou; O=Alibaba (China) Technology Co., Ltd.; CN=*.market.alicloudapi.com
* start date: Mar 11 08:27:01 2025 GMT
* expire date: Sep 4 00:00:00 2025 GMT
* subjectAltName does not match xxx-open-tst.xxxio.com
* SSL: no alternative certificate subject name matches target host name 'xxx-open-tst.xxxio.com'
* Closing connection 0
* TLSv1.2 (OUT), TLS alert, close notify (256):
curl: (60) SSL: no alternative certificate subject name matches target host name 'xxx-open-tst.xxxio.com'
More details here: https://curl.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
错误原因
- 错误信息显示:
SSL: no alternative certificate subject name matches target host name 'xxx-open-tst.xxxio.com'
- 服务器的证书是为
*.market.alicloudapi.com
签发的,而你尝试访问的主机名是xxx-open-tst.xxxio.com
,两者不匹配。 - 这是 HTTPS 的安全机制,用于防止中间人攻击
- curl 默认会验证服务器的 SSL 证书,包括检查证书是否由受信任的机构颁发、是否过期以及主机名是否匹配。
- 由于主机名不匹配,curl 拒绝了连接。
- TLS 握手本身是成功的(使用了
TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
),但证书验证失败导致连接关闭。
curl 它的功能非常强大,命令行参数多达几十种。可以掌握了解一下。
Curl 介绍
curl
(Client for URLs)是一个支持多种协议(如 HTTP、HTTPS、FTP、SFTP、SCP、LDAP 等)的命令行工具,用于在终端中传输数据。它的最大特点是无需图形界面即可与远程服务器交互,非常适合脚本自动化、API 调试、网络测试等场景
curl weread.qq.com/
选项 | 功能 | 案例 |
---|---|---|
-X |
指定请求方法(GET、POST、PUT、DELETE 等) | $ curl -X POST www.example.com |
-H |
添加请求头(Header) | |
-d |
发送数据(通常用于 POST/PUT) | |
-o |
将输出保存到文件 | |
-O |
以远程文件名保存下载内容 | |
-I |
只获取响应头信息 | |
-L |
自动跟随重定向 | |
-u |
指定用户名和密码(基本认证) | |
-F |
发送 multipart/form-data 数据(上传文件) | |
-V |
参数输出通信的整个过程,用于调试。 | $ curl -v www.example.com |
--trace |
用于调试,还会输出原始的二进制数据 | $ curl --trace - www.example.com |
使用案例详情
查看请求详细信息(调试模式) -V
你想查看完整的请求和响应过程,包括 Headers 和 Body:
- 解析的IP地址
- 建立的TCP连接
- TLS握手过程
- 发送的精确请求头
- 接收到的响应头
- 响应体
arduino
curl -v https://example.com

发送 JSON 数据调用 RESTful API
json
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{"name":"Alice", "email":"alice@example.com"}' \
https://api.example.com/v1/users
说明 :-H
设置请求头,-d
发送 JSON 数据体。
强制使用 IPv4 或 IPv6
bash
# 强制使用 IPv4
curl -4 https://example.com
# 强制使用 IPv6
curl -6 https://example.com
自动重试失败请求
当网络不可靠时,可以配置 curl
自动重试。(适用脚本中)
ruby
curl --retry 3 --retry-delay 5 https://flaky-api.example.com/data
载大文件并断点续传
但网络不稳定,希望支持断点续传
bash
# 第一次下载
curl -C - -O http://mirror.example.com/ubuntu.iso
# 如果中断,再次运行相同命令可继续下载
说明 :-C -
启用断点续传,-O
使用远程文件名保存
模拟浏览器行为(User-Agent + Referer)
有些网站会检查请求来源是否为真实浏览器,你可以通过设置 User-Agent 和 Referer 来伪装:
arduino
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" \
-e "https://www.google.com/" \
https://example.com
-A
设置 User-Agent
-e
设置 Referer 头
适用场景:爬虫伪装、绕过简单反爬机制
OAuth2 Bearer Token 调用 API
很多现代 API 使用 OAuth2 认证方式,你可以在 curl
中轻松携带 token
bash
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
https://api.example.com/v2/data
用 curl
来测量请求耗时,用于性能监控或优化分析
perl
curl -w "Time Connect: %{time_connect}\nTime AppConnect: %{time_appconnect}\nTime Redirect: %{time_redirect}\nTime Total: %{time_total}\n" -o /dev/null -s https://example.com

更多场景
场景 | 用法 |
---|---|
网络不稳定的情况下,设置超时可以防止命令长时间挂起 | 连接超时 5 秒,响应超时 10 秒curl --connect-timeout 5 -m 10 slow-api.example.com |
在生产环境中下载大文件,但不想占用全部带宽 | # 限制下载速度为100KB/scurl --limit-rate 100K -O example.com/largefile.i...# 设置超时和最大连接时间curl --connect-timeout 30 --max-time 300 -O example.com/largefile.i... |
测试一个使用自签名证书的开发服务器 | # 忽略证书验证(仅用于测试环境)curl -k dev.example.com# 指定自定义CA证书curl --cacert /path/to/custom-ca.pem dev.example.com# 检查证书详细信息curl --verbose --cert-status example.com |
当然还有很多其他用法,可以通过AI进行探索,不再赘述。
参考资源
官网地址:curl.se/docs/
curl 的用法指南: www.ruanyifeng.com/blog/2019/0...