如何从curl或wget调用基于表单的身份验证

发布于 2024-11-28 04:12:44 字数 306 浏览 4 评论 0原文

我想使用 curlwget 调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2024-12-05 04:12:44

我能够使用 wget 加载 cookies 找到部分解决方案。
这是用于导出 Cookie 的 Google Chrome 扩展程序 Chrome Google 导出扩展程序
使用此 cookie 和 wget

  1. 安装 Google Chrome 扩展。
  2. 通过 Google Chrome 登录网站
  3. 使用 Chrome 扩展程序导出 Cookie
  4. 某个地方可能与 wget 或桌面相同。
  5. 现在运行以下 wget 命令,其中包含 cookies.txt 的完整路径

示例

wget -x --load-cookies C:\Users\Documents\GnuWin32\bin\cookies.txt http://example.com/some_interesting/article/1.html

祝你好运

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

  1. Install Google Chrome Extension.
  2. Login to site via Google Chrome
  3. Export Cookie using Chrome extension
  4. Somewhere may be same place where wget or desktop.
  5. now run following wget command with full path to cookies.txt

Example

wget -x --load-cookies C:\Users\Documents\GnuWin32\bin\cookies.txt http://example.com/some_interesting/article/1.html

Good Luck

浪荡不羁 2024-12-05 04:12:44

郑重声明: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 pair username=User&password=Pwd123 (no end-of-line!) does the job.

Then --load-cookies /tmp/cookiefile should be accepted by the site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文