如何在 ruby​​ 中设置自定义用户代理 [英] How to set a custom user agent in ruby

查看:60
本文介绍了如何在 ruby​​ 中设置自定义用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是通过自动化测试 URL 上的不同用户代理.我正在使用 ruby​​ 进行编码,并且我一直在尝试使用以下方法设置用户代理,但它似乎无法识别用户代理.

I've a task to test different user agents on a URL through automation. I'm using ruby to code, and I've been trying to set an user agent using the following method, but it doesn't seem to recognize the user agent.

@http = Net::HTTP.new(URL)
response = @http.request_get(URL, {'User-Agent' => useragent})  

有没有其他方法可以做到这一点,或者我做错了什么?

Is there any other way to do this, or what am I doing wrong?

推荐答案

http = Net::HTTP.new("your.site.com", 80)
req = Net::HTTP::Get.new("/path/to/the/page.html", {'User-Agent' => 'your_agent'})
response = http.request(req)
puts response.body

非常适合我.

这篇关于如何在 ruby​​ 中设置自定义用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆