Estoy jugando con Ruby Mechanize con un sitio web. Pero parece que ignoró uno de los encabezados de cookies establecidos en la respuesta. Consulte mi código y el resultado. Falta la parte u=__LOGOUT__ en el resultado de agent.cookies. ¿Por qué? Gracias.

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

Este es el resultado cuando ejecuto el código superior.

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>
0
Just a learner 2 mar. 2012 a las 07:59

1 respuesta

La mejor respuesta

Porque esa cookie caduca es 'Jue, 01-Ene-1970 00:00:01 GMT'

0
pguardiario 2 mar. 2012 a las 08:28