如何在 ruby​​ 中设置自定义用户代理

如何在 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

非常适合我.