如何修改 Ruby 中的标头来复制 Firefox?
我正在尝试创建一个自动化工具,并且希望我的脚本能够模拟类似 Firefox 的标头。当前执行 HTTP get 将具有最少数量的标头。我相信方法、主持、接受。
这是我现在正在使用的方法:
def fetch(url_str, limit = 10)
raise ArguementError, 'HTTP redirect too deep' if limit == 0
res = Net::HTTP.get_response(URI.parse(url_str))
case res
when Net::HTTPSuccess then
$dpage += res.body
when Net::HTTPRedirection then
$dpage += fetch(res['location'], limit - 1)
else
puts res.error!
end
end
How can I change this to more headers?或者甚至修改已经存在的“接受”?
我尝试过使用“Net::HTTP::Get.add_field”的方法,但它导致主机发出“错误请求”响应。
当我分析数据包时,我明白了原因。标题的顺序不正确。不是按照我在代码中添加它们的顺序。
有什么想法吗?
I'm trying to create an automated tool and I'd like my script to simulate Firefox-like headers. Currently doing an HTTP get will have the minimal number of headers. I believe method, host, and accept.
This is the method I'm using right now:
def fetch(url_str, limit = 10)
raise ArguementError, 'HTTP redirect too deep' if limit == 0
res = Net::HTTP.get_response(URI.parse(url_str))
case res
when Net::HTTPSuccess then
$dpage += res.body
when Net::HTTPRedirection then
$dpage += fetch(res['location'], limit - 1)
else
puts res.error!
end
end
How can I change this to more headers? Or even modify the "Accept" that's already there?
I've tried a method using "Net::HTTP::Get.add_field" but it resulted in "Bad Request" response from the host.
When I analyzed the packets I could see why. The headers were not in the proper order. Not in the order that I was adding them in my code.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该研究一下类似 Typhoeus 的东西,它有一个 不错的 API 用于处理这些事情。
You should look into something like Typhoeus which has a nice API for handling these things.
您可以尝试mechanize。
You can give mechanize a try.