Ruby Mechanize不会根据请求传递cookie

时间:2011-09-21 18:38:57

标签: ruby cookies mechanize http-post

我遇到Ruby机械化问题,在手动发布请求后,在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 frontend =没问题。

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的问题)。

感谢您的帮助。

谢谢,克里斯

1 个答案:

答案 0 :(得分:0)

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

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