在机械化请求之间维护Cookie [英] Maintaining cookies between Mechanize requests

查看:89
本文介绍了在机械化请求之间维护Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ruby版本的Mechanize从我们不提供API的票务管理系统中提取雇主的票证.

I'm trying to use the Ruby version of Mechanize to extract my employer's tickets from a ticket management system that we're moving away from that does not supply an API.

问题是,Mechanize似乎没有在如下所示的post调用和get调用之间保留cookie:

Problem is, it seems Mechanize isn't keeping the cookies between the post call and the get call shown below:

require 'rubygems'
require 'nokogiri'
require 'mechanize'

@agent = Mechanize.new

page = @agent.post('http://<url>.com/user_session', {
                                            'authenticity_token' => '<token>',
                                            'user_session[login]' => '<login>',
                                            'user_session[password]' => '<password>',
                                            'user_session[remember_me]' => '0',
                                            'commit' => 'Login'
})

page = @agent.get 'http://<url>.com/<organization>/<repo-name>/tickets/1'
puts page.title

user_session是该站点的登录页面POST的URL,我已经证实这确实使我登录.但是从get调用返回的页面是'糟糕,您没有登录在!'页面.

user_session is the URL to which the site's login page POSTs, and I've verified that this indeed logs me in. But the page that returns from the get call is the 'Oops, you're not logged in!' page.

我已经验证了从post调用返回的页面上的click ing链接有效,但是如果没有JavaScript,我实际上无法到达需要去的地方.当然,我已经在具有相同登录名的浏览器上成功完成了此操作.

I've verified that clicking links on the page that returns from the post call works, but I can't actually get to where I need to go without JavaScript. And of course I've done this successfully on the browser with the same login.

我在做什么错了?

推荐答案

好的,这可能会对您有所帮助-首先,您使用的是哪个版本的机械化?您需要确定问题是否是由于在请求之间进行机械化而导致cookie被覆盖/清除,或者cookie是否错误/未首先设置.您可以通过在两个请求之间添加puts @agent.cookie_jar.jar来查看存储的内容.

Okay this might help you - first of all what version of mechanize are you using? You need to identify, if this problem is due to the cookies being overwritten/cleaned by mechanize between the requests or if the cookies are wrong/not being set in the first place. You can do that by adding a puts @agent.cookie_jar.jar inbetween the two requests, to see what is stored.

如果这是一个覆盖问题,则可以通过从第一个请求中收集cookie并将其应用于第二个请求来解决.有很多方法可以做到这一点:

If its a overwriting issue, you might be able to solve it by collecting the cookies from the first request, and applying them to the second. There are many ways to do this:

一种方法是只执行temp_jar = agent.cookie_jar.jar,然后遍历每个cookie,然后使用.add方法

One way is to just do a temp_jar = agent.cookie_jar.jar an then just going through each cookie and add it again using the .add method

但是-最简单的方法是仅安装最新版本的机械化2.1预发行版(许多修复程序),因为这样您就可以非常简单地完成它. 要安装最新版本,请执行gem install mechanize --pre并确保在此之后摆脱旧版本的机械化gem uninstall mechanize 'some_version',您可以简单地执行以下操作:

HOWEVER - the easiest way is by just installing the latest 2.1 pre release of mechanize (many fixes), because you will then be able to do it very simply. To install the latest do a gem install mechanize --pre and make sure to get rid of the old version of mechanize gem uninstall mechanize 'some_version' after this, you can simply do as follows:

require 'rubygems'
require 'nokogiri'
require 'mechanize'

@agent = Mechanize.new

page = @agent.post('http://<url>.com/user_session', {
                                        'authenticity_token' => '<token>',
                                        'user_session[login]' => '<login>',
                                        'user_session[password]' => '<password>',
                                        'user_session[remember_me]' => '0',
                                        'commit' => 'Login'
})
temp_jar = @agent.cookie_jar
#Do whatever you need an use the cookies again in a new session after that
@agent = Mechanize.new
@agent.cookie_jar = temp_jar

page = @agent.get 'http://<url>.com/<organization>/<repo-name>/tickets/1'
puts page.title

顺便说一句,文档在这里 http://mechanize.rubyforge.org/index.html

BTW the documentation is here http://mechanize.rubyforge.org/index.html

这篇关于在机械化请求之间维护Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆