Ruby Mechanize忽略cookie作为响应

时间:2012-03-02 03:59:31

标签: ruby cookies mechanize setcookie mechanize-ruby

我正在使用Ruby Mechanize 和一个网站。但它似乎忽略了响应中的一个set-cookie标头。请查看我的代码和结果。 agent.cookies的结果中缺少u=__LOGOUT__部分。为什么?感谢。

require 'mechanize'
require 'logger'
agent = Mechanize.new
agent.user_agent_alias = 'Windows IE 9'
agent.follow_meta_refresh = true
agent.log = Logger.new(STDOUT)

cookie = Mechanize::Cookie.new("__utmarea", "404-0-1")
cookie.domain = '.youku.com'
cookie.path = '/'
agent.cookie_jar.add(URI.parse("http://www.youku.com/"), cookie)

page = agent.get("http://www.youku.com/index_login/", :referer => "http://www.youku.com/")
puts agent.cookies.to_s

执行上面的代码时,这是结果。

F:\Dropbox\temp>ruby youku.rb
I, [2012-03-02T11:52:15.398550 #3188]  INFO -- : Net::HTTP::Get: /index_login/?referer=http%3A%2F%2Fwww.youku.com%2F
D, [2012-03-02T11:52:15.399550 #3188] DEBUG -- : request-header: accept => */*
D, [2012-03-02T11:52:15.400550 #3188] DEBUG -- : request-header: user-agent => Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
D, [2012-03-02T11:52:15.401550 #3188] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity
D, [2012-03-02T11:52:15.402550 #3188] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2012-03-02T11:52:15.403550 #3188] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2012-03-02T11:52:15.403550 #3188] DEBUG -- : request-header: cookie => __utmarea=404-0-1
D, [2012-03-02T11:52:15.404550 #3188] DEBUG -- : request-header: host => www.youku.com
I, [2012-03-02T11:52:15.699565 #3188]  INFO -- : status: Net::HTTPOK 1.1 200 OK
D, [2012-03-02T11:52:15.700565 #3188] DEBUG -- : response-header: server => Apache
D, [2012-03-02T11:52:15.701565 #3188] DEBUG -- : response-header: set-cookie => YOUKUSESSID=g9db0qlosehrvao5o825oh5455; path=/; domain=.youku.com, u=__LOGOUT__; expires=Thu, 01-Jan-1970 00:00:01 GMT;
path=/; domain=.youku.com
D, [2012-03-02T11:52:15.702565 #3188] DEBUG -- : response-header: expires => Thu, 19 Nov 1981 08:52:00 GMT
D, [2012-03-02T11:52:15.702565 #3188] DEBUG -- : response-header: cache-control => no-store, no-cache, must-revalidate
D, [2012-03-02T11:52:15.703566 #3188] DEBUG -- : response-header: pragma => no-cache
D, [2012-03-02T11:52:15.704566 #3188] DEBUG -- : response-header: vary => Accept-Encoding
D, [2012-03-02T11:52:15.704566 #3188] DEBUG -- : response-header: content-encoding => gzip
D, [2012-03-02T11:52:15.705566 #3188] DEBUG -- : response-header: content-type => text/html; charset=UTF-8
D, [2012-03-02T11:52:15.705566 #3188] DEBUG -- : response-header: content-length => 1724
D, [2012-03-02T11:52:15.706566 #3188] DEBUG -- : response-header: date => Fri, 02 Mar 2012 03:52:19 GMT
D, [2012-03-02T11:52:15.707566 #3188] DEBUG -- : response-header: age => 0
D, [2012-03-02T11:52:15.707566 #3188] DEBUG -- : response-header: connection => keep-alive
D, [2012-03-02T11:52:15.708566 #3188] DEBUG -- : response-header: x-cache => Miss from a05.www
D, [2012-03-02T11:52:15.709566 #3188] DEBUG -- : Read 1724 bytes (1724 total)
D, [2012-03-02T11:52:15.710566 #3188] DEBUG -- : gzip response
D, [2012-03-02T11:52:15.713566 #3188] DEBUG -- : saved cookie: YOUKUSESSID=g9db0qlosehrvao5o825oh5455
D, [2012-03-02T11:52:15.717566 #3188] DEBUG -- : saved cookie: u=__LOGOUT__
[__utmarea=404-0-1, YOUKUSESSID=g9db0qlosehrvao5o825oh5455]

F:\Dropbox\temp>

1 个答案:

答案 0 :(得分:0)

因为该cookie过期是'星期四,01年1月1日 - 1970年00:00:01 GMT'