在下面的脚本中,我访问了一个执行许多重定向的网站,我需要知道最后一次重定向的URL /端口。
cookie_file = Tempfile.new('cookie_jar.txt')
client.set_cookie_store(cookie_file)
client.set_auth(nil, user, pass)
client.ssl_config.verify_mode = nil
r = client.get(uri, :follow_redirect => true)
r = client.get_content(uri, :follow_redirect => true)
pp client.cookie_manager.jar
查看我可以从第二个cookie看到的cookie,这次是https://example.com:65003
。如果我再试一次,它将是一个不同的端口。
问题
如何解析HTTPClient cookie?
[#<HTTP::Cookie:name="ASPSESSIONIDQGDSCTSA",
value="DBEHBGICIKNNIBBMNAMGIMFG", domain="example.com",
for_domain=false, path="/", secure=true, httponly=false, expires=nil,
max_age=nil, created_at=2017-04-12 20:09:58 +0200,
accessed_at=2017-04-12 20:09:58 +0200
origin=https://example.com/bwtem/?follow_redirect=true>,
#<HTTP::Cookie:name="ASPSESSIONIDSGAQARRD",
value="APJDHADDOHENHCCHOLLMAHNJ", domain="example.com",
for_domain=false, path="/", secure=true, httponly=false, expires=nil,
max_age=nil, created_at=2017-04-12 20:09:57 +0200,
accessed_at=2017-04-12 20:09:58 +0200
origin=https://example.com:9000/auth/?id=7C05C91D24C4A798230A29FD28C587F3&target=https://example.com:65003&>]
答案 0 :(得分:1)
您可以使用URI模块执行此操作:
origin
=> "https://example.com:9000/auth/?id=7C05C91D24C4A798230A29FD28C587F3&target=https://example.com:65003&"
URI.decode_www_form(URI.parse(origin).query).to_h["target"]
=> "https://example.com:65003"