在 Mechanize 请求之间维护 cookie

发布于 2024-11-29 06:10:18 字数 1125 浏览 0 评论 0原文

我正在尝试使用 Mechanize 的 Ruby 版本从工单管理系统中提取我雇主的工单,我们正在放弃该系统,该系统不提供 API。

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

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 是要访问的 URL。该网站的登录页面 POST,我已经验证这确实使我登录。但是从 get 调用返回的页面是“哎呀,您还没有登录!”页。

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

我做错了什么?

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.

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 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.

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.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

海夕 2024-12-06 06:10:18

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

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

一种方法是只执行一个 temp_jar = agent.cookie_jar.jar 然后遍历每个 cookie 并使用 .add 方法再次添加它

- 这是最简单的方法只需安装最新的 2.1 预发行版 mechanize(许多修复),因为这样您就可以非常简单地做到这一点。
要安装最新版本,请执行 gem install mechanize --pre 并确保在此之后删除旧版本的 mechanize gem uninstall mechanize 'some_version',您可以简单地如下操作:

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

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.

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:

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

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

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

时常饿 2024-12-06 06:10:18

Mechanize 会自动发送从连续请求的响应中获得的 cookie。您可以使用相同的代理而无需重新更新。

require 'mechanize'

@agent = Mechanize.new
@agent.post(create_sessions_url, params, headers)
@agent.get(ticket_url)

使用 mechanize 2.7.6 进行测试。

Mechanize would automatically send cookies obtained from the response in the consecutive request. You can use the same agent without re-new.

require 'mechanize'

@agent = Mechanize.new
@agent.post(create_sessions_url, params, headers)
@agent.get(ticket_url)

Tested with mechanize 2.7.6.

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