Ruby Mechanize 不通过请求传递 cookie

发布于 12-05 23:13 字数 6238 浏览 0 评论 0原文

我遇到了 Ruby mechanize 的问题,在手动发布请求后的 302 重定向过程中,它丢失了 cookie。

1) 加载页面

agent.get(url)

日志:

I, [2011-09-21T19:50:46.077628 #5040]  INFO -- : Net::HTTP::Get: /some_site
D, [2011-09-21T19:50:46.077628 #5040] DEBUG -- : request-header: accept => */*
D, [2011-09-21T19:50:46.077628 #5040] DEBUG -- : request-header: user-agent => Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6
D, [2011-09-21T19:50:46.077628 #5040] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity
D, [2011-09-21T19:50:46.077628 #5040] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2011-09-21T19:50:46.077628 #5040] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2011-09-21T19:50:46.077628 #5040] DEBUG -- : request-header: host => site.com
I, [2011-09-21T19:50:47.965232 #5040]  INFO -- : status: Net::HTTPOK 1.1 200 OK
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: date => Wed, 21 Sep 2011 17:50:46 GMT
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: server => Apache/2.2.9 (Debian) mod_ssl/2.2.9 OpenSSL/0.9.8g PHP/5.2.17 mod_perl/2.0.4 Perl/v5.10.0
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: x-powered-by => PHP/5.2.17
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: set-cookie => frontend=9d47f1e106d4f2efcc2830988eb66610; expires=Wed, 21-Sep-2011 18:50:46 GMT; path=/; domain=site.com
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: expires => Thu, 19 Nov 1981 08:52:00 GMT
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: cache-control => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: pragma => no-cache
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: content-encoding => gzip
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: vary => Accept-Encoding,User-Agent
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: keep-alive => timeout=15, max=100
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: connection => Keep-Alive
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: transfer-encoding => chunked
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: content-type => text/html; charset=UTF-8
D, [2011-09-21T19:50:48.370832 #5040] DEBUG -- : saved cookie: frontend=9d47f1e106d4f2efcc2830988eb66610

这一切都很好,对我来说看起来不错。会话 cookie 已设置,并添加到 Mechanize cookie jar 中。

pp agent.cookies[0]

显示cookie前端=没问题。

2) 向服务器发送 POST 请求

agent.post(url,{"product" => "10000","qty" => "1"})

这不会将 cookie 发送到服务器。我收到一条错误消息(“cookie 未启用,请启用以继续”)。当指定时,Mechanize 是否仅在 POST 请求上传递 cookie?

除非我专门将 cookie 添加到 POST 请求,否则该 cookie 不会发送到服务器。

agent.post(url,{"product" => "10000","qty" => "1"},'cookie' => agent.cookies[0])

在这种情况下,记录器显示如下:

D, [2011-09-21T19:50:48.480032 #5040] DEBUG -- : request-header: cookie => frontend=9d47f1e106d4f2efcc2830988eb66610

3) 服务器执行 302 重定向。

对于重定向页面的 GET 请求,Mechanize 不会传递会话 cookie。因此,会话会丢失,并且服务器会设置新的会话 cookie。

I, [2011-09-21T19:50:49.182034 #5040]  INFO -- : follow redirect to: http://site.com/redirect/
I, [2011-09-21T19:50:49.182034 #5040]  INFO -- : Net::HTTP::Get: /redirect/
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: accept => */*
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: user-agent => Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: host => site.com
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: referer => http:/site.com/referrerlink/
I, [2011-09-21T19:50:49.728035 #5040]  INFO -- : status: Net::HTTPOK 1.1 200 OK
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: date => Wed, 21 Sep 2011 17:50:49 GMT
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: server => Apache/2.2.9 (Debian) mod_ssl/2.2.9 OpenSSL/0.9.8g PHP/5.2.17 mod_perl/2.0.4 Perl/v5.10.0
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: x-powered-by => PHP/5.2.17
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: set-cookie => frontend=c08477bb03473d68acd83ed81ed56101; expires=Wed, 21-Sep-2011 18:50:49 GMT; path=/; domain=site.com
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: expires => Thu, 19 Nov 1981 08:52:00 GMT
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: cache-control => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: pragma => no-cache
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: content-encoding => gzip
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: vary => Accept-Encoding,User-Agent
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: content-length => 6441
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: keep-alive => timeout=15, max=98
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: connection => Keep-Alive
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: content-type => text/html; charset=UTF-8

关于如何防止 Mechanize 在 302 重定向期间丢失 cookie,有什么建议吗?由于网站上使用了 javascript,除了手动 POST 请求之外,我无法执行任何操作。

Mechanize 的这种常见行为是仅在明确指定时通过手动 POST 请求发送 cookie(根据我使用 POST 请求的经验,到目前为止我还没有遇到丢失会话 cookie 的问题)。

我很感激你的帮助。

谢谢,克里斯

I have a problem with Ruby mechanize where it loses the cookie during a 302 redirect after a manual post request.

1) Load page

agent.get(url)

Log:

I, [2011-09-21T19:50:46.077628 #5040]  INFO -- : Net::HTTP::Get: /some_site
D, [2011-09-21T19:50:46.077628 #5040] DEBUG -- : request-header: accept => */*
D, [2011-09-21T19:50:46.077628 #5040] DEBUG -- : request-header: user-agent => Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6
D, [2011-09-21T19:50:46.077628 #5040] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity
D, [2011-09-21T19:50:46.077628 #5040] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2011-09-21T19:50:46.077628 #5040] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2011-09-21T19:50:46.077628 #5040] DEBUG -- : request-header: host => site.com
I, [2011-09-21T19:50:47.965232 #5040]  INFO -- : status: Net::HTTPOK 1.1 200 OK
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: date => Wed, 21 Sep 2011 17:50:46 GMT
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: server => Apache/2.2.9 (Debian) mod_ssl/2.2.9 OpenSSL/0.9.8g PHP/5.2.17 mod_perl/2.0.4 Perl/v5.10.0
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: x-powered-by => PHP/5.2.17
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: set-cookie => frontend=9d47f1e106d4f2efcc2830988eb66610; expires=Wed, 21-Sep-2011 18:50:46 GMT; path=/; domain=site.com
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: expires => Thu, 19 Nov 1981 08:52:00 GMT
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: cache-control => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: pragma => no-cache
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: content-encoding => gzip
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: vary => Accept-Encoding,User-Agent
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: keep-alive => timeout=15, max=100
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: connection => Keep-Alive
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: transfer-encoding => chunked
D, [2011-09-21T19:50:47.965232 #5040] DEBUG -- : response-header: content-type => text/html; charset=UTF-8
D, [2011-09-21T19:50:48.370832 #5040] DEBUG -- : saved cookie: frontend=9d47f1e106d4f2efcc2830988eb66610

This all works fine and looks good to me. Session cookie gets set, added to Mechanize cookie jar.

pp agent.cookies[0]

displays the cookie frontend= no problem.

2) Send POST request to server

agent.post(url,{"product" => "10000","qty" => "1"})

This does not send the cookie to the server. I receive an error message ("cookies not enabled, please enable to continue"). Does Mechanize only pass cookies on POST request when specified?

The cookie is not sent to server unless I specifically add it to POST request.

agent.post(url,{"product" => "10000","qty" => "1"},'cookie' => agent.cookies[0])

In this case, logger shows this:

D, [2011-09-21T19:50:48.480032 #5040] DEBUG -- : request-header: cookie => frontend=9d47f1e106d4f2efcc2830988eb66610

3) Server does a 302 redirect.

For the GET request of the redirect page, Mechanize does not pass the session cookie. Thus, the session gets lost and a new session cookie set by server.

I, [2011-09-21T19:50:49.182034 #5040]  INFO -- : follow redirect to: http://site.com/redirect/
I, [2011-09-21T19:50:49.182034 #5040]  INFO -- : Net::HTTP::Get: /redirect/
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: accept => */*
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: user-agent => Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: host => site.com
D, [2011-09-21T19:50:49.182034 #5040] DEBUG -- : request-header: referer => http:/site.com/referrerlink/
I, [2011-09-21T19:50:49.728035 #5040]  INFO -- : status: Net::HTTPOK 1.1 200 OK
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: date => Wed, 21 Sep 2011 17:50:49 GMT
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: server => Apache/2.2.9 (Debian) mod_ssl/2.2.9 OpenSSL/0.9.8g PHP/5.2.17 mod_perl/2.0.4 Perl/v5.10.0
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: x-powered-by => PHP/5.2.17
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: set-cookie => frontend=c08477bb03473d68acd83ed81ed56101; expires=Wed, 21-Sep-2011 18:50:49 GMT; path=/; domain=site.com
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: expires => Thu, 19 Nov 1981 08:52:00 GMT
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: cache-control => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: pragma => no-cache
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: content-encoding => gzip
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: vary => Accept-Encoding,User-Agent
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: content-length => 6441
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: keep-alive => timeout=15, max=98
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: connection => Keep-Alive
D, [2011-09-21T19:50:49.728035 #5040] DEBUG -- : response-header: content-type => text/html; charset=UTF-8

Any suggestions on how I can prevent Mechanize from losing the cookie during the 302 redirect? I am not able to anything but the manual POST request due to javascript used on the site.

And is this common behaviour of Mechanize to only send cookies with a manual POST request when explicitly specified (from my experience of using POST requests, I have not have problems with losing session cookies until now).

I appreciate your help.

Thanks, Chris

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

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

发布评论

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

评论(1

躲猫猫2024-12-12 23:13:50

我在重定向时遇到了同样的问题,重定向后 cookie 没有保存在 jar 中。我从 github 存储库下载了最新版本的 Mechanize(版本 2.0.2),问题似乎已得到解决。不太确定代码库中的哪些更改解决了这个问题,但 cookie 现在似乎在重定向后保存。

当您尝试此修复时,请确保您使用的是 gem 2.0.2 版本,而不是其他本地安装的 gem 版本。这让我有一段时间。 :)

I ran into the same problem with redirects, where the cookies weren't being saved in the jar after a redirect. I downloaded the latest version of Mechanize from the github repository (version 2.0.2) and the issue seems to be fixed. Not exactly sure what change in the code base fixed this problem, but the cookies now seem to be saving after redirects.

When you are trying this fix, make sure that you're using version 2.0.2 of the gem, and not another locally installed version of the gem. That got me for a while. :)

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