How to set a custom user agent in ruby

后端 未结 4 519
我在风中等你
我在风中等你 2020-12-31 01:32

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 i

相关标签:
4条回答
  • 2020-12-31 01:44

    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.

    0 讨论(0)
  • 2020-12-31 01:46

    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)

    0 讨论(0)
  • 2020-12-31 01:55
    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.

    0 讨论(0)
  • 2020-12-31 01:59

    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

    0 讨论(0)
提交回复
热议问题