Get Mechanize to handle cookies from an arbitrary POST (to log into a website programmatically)

荒凉一梦 提交于 2019-12-06 14:54:09

You can use something like this to login and save the cookie so you won't have to do it again. Of course you will need to come up with your own logic to post it directly but this is how I use Mechanize's built in cookie_jar method to save cookies.

if !agent.cookie_jar.load('cookies.yml')
  page = agent.get('http://site.com')

  form = page.forms.last
  form.email = 'email'
  form.password = 'password'

  page = agent.submit(form)

  agent.cookie_jar.save_as('cookies.yml')
end

I would avoid Net::HTTP; try with:

post(url, query={}, headers={})

directly from Mechanize class.

I often use the FireFox HttpFox extension to figure out what exactly is going on for these kind of problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!