Ruby 的 open-uri 和 cookies
我想存储来自一个 open-uri 调用的 cookie 并将它们传递给下一个调用。我似乎找不到合适的文档来执行此操作。如果您能告诉我正确的方法,我将不胜感激。
注意:w3.org 不是实际的网址,但它更短;假装饼干在这里很重要。
h1 = open("http://www.w3.org/")
h2 = open("http://www.w3.org/People/Berners-Lee/", "Cookie" => h1.FixThisSpot)
2 点后更新:虽然这并不是反问句,但我保证这是可能的。 风滚草之后更新:看(答案),这是可能的。我花了很长时间,但它有效。
I would like to store the cookies from one open-uri call and pass them to the next one. I can't seem to find the right docs for doing this. I'd appreciate it if you could tell me the right way to do this.
NOTES: w3.org is not the actual url, but it's shorter; pretend cookies matter here.
h1 = open("http://www.w3.org/")
h2 = open("http://www.w3.org/People/Berners-Lee/", "Cookie" => h1.FixThisSpot)
Update after 2 nays: While this wasn't intended as rhetorical question I guarantee that it's possible.
Update after tumbleweeds: See (the answer), it's possible. Took me a good while, but it works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我以为有人会知道,但我想这通常不是用
open-uri
完成的。这是一个丑陋的版本,既不检查隐私、过期、正确的域名,也不检查正确的路径:
是的,它有效。不,它不漂亮,也不完全符合建议,也不处理多个 cookie(按原样)。
显然,HTTP 是一个非常简单的协议,
open-uri
可以让您充分利用它。我想我真正需要知道的是如何从h1
请求中获取 cookie,以便可以将其传递给h2
请求(这部分我已经知道并展示了)。令人惊讶的是,有多少人基本上想通过告诉我不要使用 open-uri 来回答,而其中只有一个人展示了如何将一个请求中设置的 cookie 传递到下一个请求。I thought someone would just know, but I guess it's not commonly done with
open-uri
.Here's the ugly version that neither checks for privacy, expiration, the correct domain, nor the correct path:
Yes, it works. No it's not pretty, nor fully compliant with recommendations, nor does it handle multiple cookies (as is).
Clearly, HTTP is a very straight-forward protocol, and
open-uri
lets you at most of it. I guess what I really needed to know was how to get the cookie from theh1
request so that it could be passed to theh2
request (that part I already knew and showed). The surprising thing here is how many people basically felt like answering by telling me not to useopen-uri
, and only one of those showed how to get a cookie set in one request passed to the next request.您需要添加一个“Cookie”标头。
我不确定 open-uri 是否可以做到这一点,但可以使用 Net::HTTP 来完成。
You need to add a "Cookie" header.
I'm not sure if open-uri can do this or not, but it can be done using Net::HTTP.
谢谢马修·申克尔,你的回答非常有用。使用 Net::HTTP 我成功了
Thanks Matthew Schinckel your answer was really useful. Using Net::HTTP I was successful
根据您想要实现的目标,请查看 webrat。我知道它通常用于测试,但它也可以访问实时网站,并且它可以完成您的网络浏览器可以为您做的很多事情,例如在请求之间存储 cookie 并遵循重定向。
Depending on what you are trying to accomplish, check out webrat. I know it is usually used for testing, but it can also hit live sites, and it does a lot of the stuff that your web browser would do for you, like store cookies between requests and follow redirects.
如果您使用 open-uri,则必须在读取时解析元标头并在提交请求时添加 cookie 标头来滚动自己的 cookie 支持。考虑使用 httpclient http://raa.ruby-lang.org/project/httpclient/ 或类似 mechanize 的东西 http://mechanize.rubyforge.org/ 因为它们内置了 cookie 支持在。
you would have to roll your own cookie support by parsing the meta headers when reading and adding a cookie header when submitting a request if you are using open-uri. Consider using httpclient http://raa.ruby-lang.org/project/httpclient/ or something like mechanize instead http://mechanize.rubyforge.org/ as they have cookie support built in.
对于需要符合标准的 cookie 处理的用户,可以在此处找到 RFC 2109 和 RFC 2965 cookie jar 实现。
https://github.com/dwaite/cookiejar
There is a RFC 2109 and RFC 2965 cookie jar implementation to be found here for does that want standard compliant cookie handling.
https://github.com/dwaite/cookiejar