CURL 访问需要从不同页面登录的页面 [英] CURL to access a page that requires a login from a different page

查看:29
本文介绍了CURL 访问需要从不同页面登录的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个页面:xyz.com/axyz.com/b.当且仅当我先登录 xyz.com/a 时,我才能访问 xyz.com/b.如果访问 xyz.com/b 而不通过另一个,我只会通过浏览器拒绝访问(没有重定向到登录).一旦我登录 xyz.com/a,我就可以访问另一个.

I have 2 pages: xyz.com/a and xyz.com/b. I can only access xyz.com/b if and only if I login to xyz.com/a first. If accessing xyz.com/b without going through the other, I simply get access denied (no redirect to login) via the browser. Once I login at xyz.com/a, I can access the other.

我的问题是使用 curl 命令执行此操作.我可以使用 curl 成功登录到 xyz.com/a,但随后尝试 xyx.com/b 并且我的访问被拒绝.

My problem is doing this using the curl command. I can login successfully to xyz.com/a using curl, but then try xyx.com/b and I get access denied.

我使用以下内容:

curl --user user:pass https://xyz.com/a  #works ok
curl https://xyz.com/b #doesn't work

我尝试将第二行与 & 一起使用没有用户/密码部分,仍然不起作用.两个页面都使用相同的 CA,所以这不是问题.有什么建议?谢谢

I've tried using the second line with & without the user/password part and still doesn't work. Both pages uses the same CA, so that's not a problem. Any suggestions? Thanks

推荐答案

该网站可能使用 cookies 来存储您的会话信息.当你跑

The web site likely uses cookies to store your session information. When you run

curl --user user:pass https://xyz.com/a  #works ok
curl https://xyz.com/b #doesn't work

curl 在两个单独的会话中运行两次.因此,当第二个命令运行时,第一个命令设置的 cookie 不可用;这就像您在一个浏览器会话中登录页面 a,并尝试在另一个浏览器会话中访问页面 b.

curl is run twice, in two separate sessions. Thus when the second command runs, the cookies set by the 1st command are not available; it's just as if you logged in to page a in one browser session, and tried to access page b in a different one.

你需要做的是保存第一个命令创建的cookies:

What you need to do is save the cookies created by the first command:

curl --user user:pass --cookie-jar ./somefile https://xyz.com/a

然后在运行第二个时读回它们:

and then read them back in when running the second:

curl --cookie ./somefile https://xyz.com/b

或者,您可以尝试在同一个命令中下载这两个文件,我认为这将使用相同的 cookie.

Alternatively you can try downloading both files in the same command, which I think will use the same cookies.

这篇关于CURL 访问需要从不同页面登录的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆