如何从curl或wget调用基于表单的身份验证
我想使用 curl
和 wget
调用 url。
我尝试过:
wget --save-cookies cookies.txt --post-data 'user=foo&password=bar' http://server.com/Login
wget --load-cookies cookies.txt -p http://server.com/interesting/article.jsp
但是登录表单不接受发布数据;我还能用其他方法做到这一点吗?
I want to call an url using curl
and wget
.
I tried:
wget --save-cookies cookies.txt --post-data 'user=foo&password=bar' http://server.com/Login
wget --load-cookies cookies.txt -p http://server.com/interesting/article.jsp
But the login form is not accepting post data; any other way I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够使用 wget 加载 cookies 找到部分解决方案。
这是用于导出 Cookie 的 Google Chrome 扩展程序 Chrome Google 导出扩展程序
使用此 cookie 和 wget
示例
祝你好运
I was able to find partial solution using load cookies with wget.
Here is Google Chrome extension to export cookies Chrome Google Export Extension
use this cookie with wget
Example
Good Luck
郑重声明:Mowgli 的解决方案对我不起作用(我的环境中没有 Chrome),因此经过一番挖掘后我发现了问题:
基于表单的身份验证通常会设置会话 cookie,但
wget
不会保存它们默认情况下。因此,使用如下命令:
wget --keep-session-cookies --save-cookies /tmp/cookiefile --post-data='username=User&password=Pwd123'
或者更安全一些:
wget --keep-session-cookies --save-cookies /tmp/cookiefile --post-file creds
其中文件
creds
将包含相同的对username=User&password=Pwd123
(没有行尾!)来完成这项工作。然后
--load-cookies /tmp/cookiefile
应该被网站接受。For the record: Mowgli's solution did not work for me (no Chrome in my environment), so after some digging I found the issue:
form-based authentication usually sets session cookies, but
wget
does not save them by default.So using a command like:
wget --keep-session-cookies --save-cookies /tmp/cookiefile --post-data='username=User&password=Pwd123' <login URL>
or somewhat safer:
wget --keep-session-cookies --save-cookies /tmp/cookiefile --post-file creds <login URL>
where the file
creds
would contain the same pairusername=User&password=Pwd123
(no end-of-line!) does the job.Then
--load-cookies /tmp/cookiefile
should be accepted by the site.