开发者

Ruby Mechanize does not pass cookie with request

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 =&开发者_如何学Cgt; 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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜