【问题标题】:How to parse cookies in HTTPClient?如何在 HTTPClient 中解析 cookie?
【发布时间】:2017-04-12 18:19:36
【问题描述】:

在下面的脚本中,我访问了一个执行许多重定向的网站,我需要知道最后一个重定向的 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&>]

【问题讨论】:

  • Cookies 通常是加盐/加密/签名的,所以除非你有秘密,否则你将无法解密它。
  • 这是target 的值,我正在寻找纯文本。所以问题是如何访问它。
  • 您的意思是要从origin 的值中获取target 查询参数?
  • 是的,在这种情况下,我想在变量中以 https://example.com:65003 结尾。

标签: ruby parsing cookies httpclient


【解决方案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"

【讨论】:

    猜你喜欢
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多