有没有办法使用命令行cURL跟踪重定向?

本文讨论如何在命令行中使用cURL工具跟踪HTTP重定向。提到了使用位置标头标志以及限制重定向次数的参数,并提供了具体的cURL命令示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Is there a way to follow redirects with command line cURL?

I know that in a php script: 我知道在php脚本中:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

will follow redirects. 将遵循重定向。 Is there a way to follow redirects with command line cURL? 有没有办法使用命令行cURL跟踪重定向?


#1楼

参考:https://stackoom.com/question/1FW6s/有没有办法使用命令行cURL跟踪重定向


#2楼

使用位置标头标志:

curl -L <URL>


#3楼

I had a similar problem. 我遇到了类似的问题。 I am posting my solution here because I believe it might help one of the commenters. 我在这里发布我的解决方案,因为我相信它可能会对评论者之一有所帮助。

For me, the obstacle was that the page required a login and then gave me a new URL through javascript. 对我来说,障碍是该页面需要登录,然后通过javascript给我一个新的URL。 Here is what I had to do: 这是我必须做的:

curl -c cookiejar -g -O -J -L -F "j_username=yourusename" -F "j_password=yourpassword" <URL>

Note that j_username and j_password is the name of the fields for my website's login form. 请注意,j_username和j_password是我网站登录表单的字段名称。 You will have to open the source of the webpage to see what the 'name' of the username field and the 'name' of the password field is in your case. 您必须打开网页的来源,以查看用户名字段的“名称”和密码字段的“名称”是什么。 After that I go an html file with java script in which the new URL was embedded. 之后我去了一个带有java脚本的html文件,其中嵌入了新的URL。 After parsing this out just resubmit with the new URL: 解析完后,只需使用新URL重新提交:

curl -c cookiejar -g -O -J -L -F "j_username=yourusename" -F "j_password=yourpassword" <NEWURL>


#4楼

As said, to follow redirects you can use the flag -L or --location : 如上所述, 要遵循重定向,您可以使用标志-L--location

curl -L http://www.example.com

But, if you want limit the number of redirects , add the parameter --max-redirs 但是, 如果要限制重定向的数量 ,请添加参数--max-redirs

 --max-redirs <num> 

Set maximum number of redirection-followings allowed. 设置允许的最大重定向次数。 If -L , --location is used, this option can be used to prevent curl from following redirections "in absurdum". 如果使用-L , - --location ,则此选项可用于防止curl跟随“在荒谬中”重定向。 By default, the limit is set to 50 redirections. 默认情况下,限制设置为50次重定向。 Set this option to -1 to make it limitless. 将此选项设置为-1可使其无限制。 If this option is used several times, the last one will be used. 如果多次使用此选项,将使用最后一个选项。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值