how to call a form based authentication from curl or wget
I want to call an url using curl and wget
开发者_JAVA技巧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 try this?
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
- Install Google Chrome Extension.
- Login to site via Google Chrome
- Export Cookie using Chrome extension
- Somewhere may be same place where wget or desktop.
- 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
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 saves 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.
精彩评论