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

发布于 2024-11-03 06:13:25 字数 237 浏览 3 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

〗斷ホ乔殘χμё〖 2024-11-10 06:13:25
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

对我来说效果很好。

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

Works great for me.

夏末的微笑 2024-11-10 06:13:25

还有另一个对我有用的:

require 'open-uri'
html = open('http://your.site.com/the/page.html', 'User-Agent' => 'Ruby').read
puts html

希望这会对您有所帮助。

Also another that work for me :

require 'open-uri'
html = open('http://your.site.com/the/page.html', 'User-Agent' => 'Ruby').read
puts html

Hope this will help you.

甜中书 2024-11-10 06:13:25

包含的 Net::HTTPHeader初始化_http_header方法:

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

HTH

The included Net::HTTPHeader has the initialize_http_header method:

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

HTH

长亭外,古道边 2024-11-10 06:13:25

我无法找到适用于 https 并提供标头的解决方案。这是一个有效的版本:

require "net/http"

uri = URI("https://pokemongolive.com/events/community-day/")

request = Net::HTTP::Get.new(uri)
request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"

response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => (uri.scheme == 'https')) {|http|
  http.request(request)
}

puts response.code
puts response.body

正在使用的 Ruby 版本是:ruby 2.6.3p62(2019-04-16 修订版 67580)

I wasn't able to find a solution that works for both https and supplying a header. Here is a version that works:

require "net/http"

uri = URI("https://pokemongolive.com/events/community-day/")

request = Net::HTTP::Get.new(uri)
request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"

response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => (uri.scheme == 'https')) {|http|
  http.request(request)
}

puts response.code
puts response.body

The Ruby version being used was: ruby 2.6.3p62 (2019-04-16 revision 67580)

作妖 2024-11-10 06:13:25
require 'net/https'

uri = URI('https://example.com')
params = {
  'user-agent' => '...'
}

res = Net::HTTP.get_response(uri, params)
puts res.code
puts res.body
require 'net/https'

uri = URI('https://example.com')
params = {
  'user-agent' => '...'
}

res = Net::HTTP.get_response(uri, params)
puts res.code
puts res.body
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文