curl 网络测试常用方法

一、基础网络测试

1. 连通性测试

bash 复制代码
curl -I http://example.com  # 仅显示响应头(快速检查状态码)
curl -v https://api.example.com  # 显示详细过程(握手、头信息)

2. 超时控制

bash 复制代码
curl --connect-timeout 5 -m 10 http://example.com  # 连接超时5秒,总超时10秒

3. DNS解析测试

bash 复制代码
curl --resolve example.com:80:1.2.3.4 http://example.com  # 强制指定IP解析

二、性能分析

1. 速度测试

bash 复制代码
curl -o /dev/null -s -w "时间: %{time_total}s\n速度: %{speed_download}B/s" https://example.com/file.zip

2. 连接阶段分析

bash 复制代码
curl -w "
DNS解析: %{time_namelookup}s
建立连接: %{time_connect}s
SSL握手: %{time_appconnect}s
首字节: %{time_starttransfer}s
总时间: %{time_total}s\n" -o /dev/null -s https://example.com

三、特殊场景

1. HTTPS证书检查

bash 复制代码
curl -vI https://example.com 2>&1 | grep -E "SSL|CN"  # 查看证书信息
curl --cacert ./ca.pem https://intranet.com  # 自定义CA证书

2. 代理测试

bash 复制代码
curl -x http://proxy:8080 -U user:pass http://example.com  # 通过代理访问

3. API调试

bash 复制代码
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com
curl -H "Authorization: Bearer token" https://api.example.com/data

4. 文件传输

bash 复制代码
curl -u ftpuser:ftppass -T file.txt ftp://ftp.example.com # FTP上传 curl -O http://example.com/image.jpg # 下载文件保存原名

四、高级诊断

1. IPv4/IPv6强制

bash 复制代码
curl -4 http://example.com  # 强制IPv4
curl -6 http://example.com  # 强制IPv6

2. Cookie操作

bash 复制代码
curl -c cookies.txt http://login.com  # 保存Cookie
curl -b cookies.txt http://dashboard.com  # 携带Cookie

3. 流量控制

bash 复制代码
curl --limit-rate 100K http://example.com/largefile.iso  # 限速100KB/s

五、实用组合场景

场景 命令示例
CDN节点检测 curl -s -o /dev/null -w "%{remote_ip}" http://cdn.com
重定向跟踪 curl -Ls -w "最终URL: %{url_effective}" -o /dev/null http://bit.ly/short
负载均衡测试 `for i in {1..10}; do curl -s lb.example.com grep Server; done`
压缩测试 `curl -H "Accept-Encoding: gzip" -I example.com grep Content-Encoding`
持续监控 while true; do curl -s -o /dev/null -w "%{http_code} %{time_total}\n" http://service; sleep 2; done

六、常用输出格式化变量

bash 复制代码
curl -w "
响应码: %{http_code}
重定向URL: %{redirect_url}
SSL验证: %{ssl_verify_result}
本地IP: %{local_ip}
远端IP: %{remote_ip}
下载大小: %{size_download} bytes
" -o /dev/null -s http://example.com

调试技巧 :使用 -v 时,> 表示发送数据,< 表示接收数据,* 表示附加信息。结合 grep 可快速过滤关键信息(如 grep -E '> Host:|< HTTP')。

相关推荐
一 乐16 小时前
绿色农产品销售|基于springboot + vue绿色农产品销售系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·宠物
3***688417 小时前
Spring Boot中使用Server-Sent Events (SSE) 实现实时数据推送教程
java·spring boot·后端
C***u17617 小时前
Spring Boot问题总结
java·spring boot·后端
上进小菜猪17 小时前
基于 YOLOv8 的人体与行人检测智能识别实战 [目标检测完整源码]
后端
Elieal17 小时前
5 种方式快速创建 SpringBoot 项目
java·spring boot·后端
c***693017 小时前
Spring Boot实时推送技术详解:三个经典案例
spring boot·后端·状态模式
Mr -老鬼18 小时前
Rust适合干什么?为什么需要Rust?
开发语言·后端·rust
123445218 小时前
Agent入门实战-一个题目生成Agent
人工智能·后端
IT_陈寒18 小时前
Java性能调优实战:5个被低估却提升30%效率的JVM参数
前端·人工智能·后端
快手技术18 小时前
AAAI 2026|全面发力!快手斩获 3 篇 Oral,12 篇论文入选!
前端·后端·算法