Linux CURL 访问需要从不同页面登录的页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12399087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:20:14  来源:igfitidea点击:

CURL to access a page that requires a login from a different page

linuxbashcookiescurlhttp-authentication

提问by ms1013

I have 2 pages: xyz.com/aand xyz.com/b. I can only access xyz.com/bif and only if I login to xyz.com/afirst. If accessing xyz.com/bwithout 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.

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

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

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

I use the following:

我使用以下内容:

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

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

我试过在没有用户/密码部分的情况下使用带有 & 的第二行,但仍然无法正常工作。两个页面都使用相同的 CA,所以这不是问题。有什么建议?谢谢

采纳答案by Mechanical snail

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

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

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

curlis 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 ain one browser session, and tried to access page bin a different one.

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

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

您需要做的是保存第一个命令创建的 cookie:

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

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

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

回答by Joe Mills

After some googling I found this:

经过一番谷歌搜索后,我发现了这个:

curl -c cookie.txt -d "LoginName=someuser" -d "password=somepass" https://oursite/a
curl -b cookie.txt https://oursite/b

No idea if it works, but it might lead you in the right direction.

不知道它是否有效,但它可能会引导你走向正确的方向。

回答by user

Also you might want to log in via browser and get the command with all headers including cookies:

此外,您可能希望通过浏览器登录并获取包含 cookie 在内的所有标头的命令:

Open the Network tab of Developer Tools, log in, navigate to the needed page, use "Copy as cURL".

打开开发者工具的网络选项卡,登录,导航到需要的页面,使用“Copy as cURL”。

screenshot

screenshot