首页 / 用例 / curl
如何用 curl 使用代理
curl 原生支持 HTTP、HTTPS 和 SOCKS 代理。下面是每种类型的确切参数,以及一条直接从免费、每小时验证的列表里抓可用代理的命令。
各类型代理的参数
| 代理类型 | curl 命令 |
|---|---|
| HTTP / HTTPS | curl -x http://host:port URL |
| SOCKS5(远程 DNS) | curl --socks5-hostname host:port URL |
| SOCKS5(本地 DNS) | curl --socks5 host:port URL |
| SOCKS4 | curl --socks4 host:port URL |
务必加上 --max-time 10,让 curl 在失效代理上快速放弃而不是一直卡住。
export http_proxy="http://1.2.3.4:8080" export https_proxy="http://1.2.3.4:8080" curl -I "https://example.com/" # 自动使用代理
一行抓一个可用代理
下载列表,取第一条(最快)直接用:
proxy="$(curl -sL https://raw.githubusercontent.com/xyzs996/free-proxy-health-list/main/proxies/protocols/http/data.txt | head -n 1)" curl -x "http://$proxy" -I "https://example.com/" --max-time 10
循环直到有一个可用
curl -sL https://raw.githubusercontent.com/xyzs996/free-proxy-health-list/main/proxies/protocols/http/data.txt \
| while read -r proxy; do
if curl -x "http://$proxy" -s -o /dev/null -I "https://example.com/" --max-time 8; then
echo "可用: $proxy"; break
fi
done
常见问题
--socks5 和 --socks5-hostname 有什么区别?
--socks5 在本地解析 DNS;--socks5-hostname 通过代理解析。优先用 --socks5-hostname 避免 DNS 泄露。
curl 卡在某个代理上怎么办?
加 --max-time(可再加 --connect-timeout)。免费代理时刻失效,要快速超时并换下一条。
不写脚本能测代理吗?
可以 —— 测试命令生成器能为任意代理和协议生成可直接运行的 curl 命令。