快速回答
如何用 curl 使用代理?
HTTP 代理用 curl -x http://USERNAME:[email protected]:7777 https://api.ipify.org,SOCKS5 代理用 --socks5-hostname gw.crawlrelay.com:7778 搭配 -U USERNAME:PASSWORD。CrawlRelay 住宅代理用户名包含国家和粘性会话字段,如 USERNAME-country-de-session-t1-ttl-10。来自 US$0.50/GB,支持加密货币支付,且无需 KYC。
curl 需要的参数
-x / --proxy | 代理 URL:http://host:port,可选包含凭证 |
|---|---|
-U / --proxy-user | 代理凭证作为 USERNAME:PASSWORD,不放在 URL 中 |
--socks5-hostname | SOCKS5 代理,出口处解析 DNS |
-v | 显示 CONNECT 协议交换和代理状态行 |
-w | 打印时间、大小和 %{http_connect},代理的 CONNECT 状态 |
--max-time,--connect-timeout | 总时间和连接时间限制,秒为单位 |
基本代理命令有哪些?
每个命令都会显示网站看到的出口 IP。
# Rotating exit, any country
curl -x http://gw.crawlrelay.com:7777 -U USERNAME:PASSWORD https://api.ipify.org
# Country and city (city availability varies)
curl -x http://gw.crawlrelay.com:7777 -U USERNAME-country-us-city-chicago:PASSWORD https://api.ipify.org
# Sticky: run it twice, the IP stays the same for up to 10 minutes (it can change earlier)
curl -x http://gw.crawlrelay.com:7777 -U USERNAME-country-de-session-t1-ttl-10:PASSWORD https://api.ipify.org
# SOCKS5, DNS resolved at the exit
curl --socks5-hostname gw.crawlrelay.com:7778 -U USERNAME-country-gb:PASSWORD https://api.ipify.org如何使用 curl 调试代理连接?
运行命令含 -v 并查看 CONNECT 附近的行。一个有效的通道会显示 HTTP/1.1 200 Connection established,然后是与网站的 TLS 握手。该行上的其他状态来自代理:407 表示凭证问题,502 或 504 表示出口或目标问题。下面的 -w 格式将相同信息打印在一行,适合循环和持续集成检查。
# Full exchange, including the proxy's answer to CONNECT
curl -v -x http://gw.crawlrelay.com:7777 -U USERNAME:PASSWORD https://example.com/ -o /dev/null
# One line: proxy CONNECT status, HTTP status, compressed bytes and timings
curl -s --compressed -o /dev/null -x http://gw.crawlrelay.com:7777 -U USERNAME-country-us:PASSWORD \
-w 'connect=%{http_connect} http=%{http_code} bytes=%{size_download} tcp=%{time_connect}s tls=%{time_appconnect}s ttfb=%{time_starttransfer}s total=%{time_total}s\n' \
https://example.com/如何检查旋转和会话是否正常?
多次请求 IP。没有会话 ID,意味着每次都是新连接,通常显示不同 IP。有会话 ID 时,IP 会保持不变直到 TTL 到期,尽管可能会提前变更。
echo "rotating:"
for i in 1 2 3 4 5; do
curl -s -x http://gw.crawlrelay.com:7777 -U USERNAME-country-us:PASSWORD https://api.ipify.org; echo
done
echo "sticky:"
for i in 1 2 3 4 5; do
curl -s -x http://gw.crawlrelay.com:7777 -U USERNAME-country-us-session-loop1-ttl-5:PASSWORD https://api.ipify.org; echo
done如何在 Windows 上使用 curl 配合代理?
调用 curl.exe,不要调用 curl。在 Windows PowerShell 5.1 中,curl 是 Invoke-WebRequest 的别名,但不支持 -x 或 -U。Windows 10 和 11 附带真正的 curl 命令为 curl.exe。在 PowerShell 中,对含特殊字符的参数加引号,密码中单引号内写字面 $。
curl.exe -x http://gw.crawlrelay.com:7777 -U 'USERNAME-country-fr:PASSWORD' https://api.ipify.org如何为每条命令统一设置代理?
curl 会读取 https_proxy 和 http_proxy 环境变量,许多工具也会读取。为 shell 会话设置,使用 NO_PROXY 排除不需走代理的主机,如您的 API。避免将密码放在会导出给共享机器所有子进程的变量中。
export https_proxy="http://USERNAME-country-us:[email protected]:7777"
export http_proxy="$https_proxy"
export NO_PROXY="localhost,127.0.0.1,.internal.example"
curl https://api.ipify.org # now uses the proxy
unset https_proxy http_proxy # back to directcurl 和代理:常见问题
--socks5 和 --socks5-hostname 有何区别?
使用 --socks5 时,curl 在本机解析主机名,然后将 IP 发送到代理。使用 --socks5-hostname 时,curl 发送主机名,由代理解析。抓取时用 --socks5-hostname,保证 DNS 响应匹配出口国家,本地解析器也无法看到您抓取的域名。
为什么 curl 显示“CONNECT tunnel failed, response 407”?
代理拒绝了凭证,或没有收到凭证。检查用户名和密码,若在 -x URL 中包含特殊字符,请 URL 编码,或改用 -U 传递。407 页面](/docs/errors/407-proxy-authentication-required/)列出了其他原因。
如何查看通过代理页面消耗的字节数?
加入 --compressed -o /dev/null -w '%{size_download}\n'。--compressed 会让 curl 像您的抓取器一样请求 gzip 或 brotli,%{size_download} 则统计压缩后的传输字节数。不加它,服务器会直接发送未压缩页面,数据量可能大数倍。在真实页面中跑几次,估算每 1000 页的流量。
curl 能否通过代理使用 HTTP/2 或 HTTP/3?
如果网站支持,HTTP/2 可在 CONNECT 隧道中工作,因为隧道实现端到端 TLS。HTTP/3 运行于 UDP,而通过 HTTP 代理的 CONNECT 隧道无法承载 UDP,故请通过代理网关使用 HTTP/2 或 HTTP/1.1。
命令行测试您的目标
US$1.50,适用于1 GB,满足上千次 curl 测试需求。