乐闻世界logo
搜索文章和话题

cURL 如何处理 HTTP 重定向?

3月6日 23:08

重定向处理是 cURL 访问 Web 资源时的常见需求。正确处理重定向能确保请求到达最终目标地址。

基本重定向处理

bash
# 不跟随重定向(默认行为) curl http://example.com # 自动跟随重定向 curl -L http://example.com curl --location http://example.com # 显示重定向过程 curl -L -v http://example.com

重定向次数限制

bash
# 限制最大重定向次数(默认 50 次) curl -L --max-redirs 5 http://example.com # 无限重定向(不推荐) curl -L --max-redirs -1 http://example.com

POST 请求重定向行为

bash
# 默认:POST 重定向后转为 GET curl -L -X POST -d "data=test" http://example.com/submit # 保持 POST 方法(RFC 7231) curl -L --post301 -X POST -d "data=test" http://example.com/submit # 保持 POST 方法(非标准) curl -L --post302 -X POST -d "data=test" http://example.com/submit curl -L --post303 -X POST -d "data=test" http://example.com/submit

重定向类型说明

状态码类型默认行为
301永久重定向POST → GET
302临时重定向POST → GET
303See OtherPOST → GET
307临时重定向保持方法
308永久重定向保持方法

查看重定向链

bash
# 显示所有重定向 URL curl -L -v http://example.com 2>&1 | grep -E "(< HTTP|< Location)" # 使用 -w 输出最终 URL curl -L -w "Final URL: %{url_effective}\n" -o /dev/null -s http://example.com # 获取重定向次数 curl -L -w "Redirects: %{num_redirects}\n" -o /dev/null -s http://example.com

常见重定向场景

bash
# HTTP 到 HTTPS 重定向 curl -L http://example.com # www 到非 www 重定向 curl -L http://www.example.com # 短链接展开 curl -L -w "Final URL: %{url_effective}\n" -o /dev/null -s https://bit.ly/shorturl # 登录后重定向 curl -L -c cookies.txt -b cookies.txt \ -X POST \ -d "username=admin&password=123456" \ http://example.com/login

高级重定向控制

bash
# 限制重定向协议 curl -L --proto-redir =https http://example.com # 重定向时保持请求头 curl -L --location-trusted \ -H "Authorization: Bearer token123" \ http://example.com/api # 查看重定向前后的 Cookie curl -L -c cookies.txt -b cookies.txt -v http://example.com 2>&1 | grep -i cookie # 重定向时修改请求头 curl -L -H "Host: newdomain.com" http://example.com

重定向调试脚本

bash
#!/bin/bash # 重定向追踪脚本 URL="http://example.com" echo "========== 重定向链追踪 ==========" echo "初始 URL: $URL" echo "" # 方法一:使用 -v 查看 curl -L -v "$URL" 2>&1 | grep -E "(< HTTP|< Location)" | head -20 echo "" echo "========== 最终信息 ==========" # 获取最终 URL FINAL_URL=$(curl -L -w "%{url_effective}" -o /dev/null -s "$URL") echo "最终 URL: $FINAL_URL" # 获取重定向次数 REDIRECTS=$(curl -L -w "%{num_redirects}" -o /dev/null -s "$URL") echo "重定向次数: $REDIRECTS" # 获取最终状态码 STATUS=$(curl -L -w "%{http_code}" -o /dev/null -s "$URL") echo "最终状态码: $STATUS"

常见问题解决

bash
# 问题 1:重定向循环 # 解决:限制重定向次数 curl -L --max-redirs 10 http://example.com # 问题 2:跨域重定向丢失认证 # 解决:使用 --location-trusted curl -L --location-trusted -H "Authorization: Bearer token" http://example.com # 问题 3:POST 数据丢失 # 解决:使用 --post302 或 --post301 curl -L --post302 -X POST -d "data=test" http://example.com # 问题 4:HTTPS 重定向失败 # 解决:确保 SSL 证书有效或使用 -k curl -L -k http://example.com # 问题 5:重定向到不同域名 # 解决:检查并更新 Host 头 curl -L -H "Host: newdomain.com" http://example.com

实战示例

bash
# 完整的重定向感知 API 调用 curl -L --max-redirs 5 \ --post302 \ -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer token123" \ -d '{"action":"submit"}' \ -w "\n最终URL: %{url_effective}\n重定向次数: %{num_redirects}\nHTTP状态: %{http_code}\n" \ http://api.example.com/submit # 短链接批量展开 for url in "bit.ly/abc" "t.co/xyz"; do echo -n "$url -> " curl -L -w "%{url_effective}" -o /dev/null -s "https://$url" echo "" done

标签:cURL