User Agent String

Hey guys,

I want my Ruby script to make a request to a server but I don’t want it
to use just the basic User Agent String. How would I go about giving my
script a User Agent string to use?

Tom A. wrote:

Hey guys,

I want my Ruby script to make a request to a server but I don’t want it
to use just the basic User Agent String. How would I go about giving my
script a User Agent string to use?

I would suggest posting what you already have and we’ll go from there.

  • Mac

Hi,

How about:

require ‘net/http’
user_agent = “you can set this part”
h = Net::HTTP.new(‘www.example.com’, 80)
resp, data = h.get(’/index.html’, {“User-Agent”=>user_agent})

Zaki

Tom A. wrote:

Hey guys,

I want my Ruby script to make a request to a server but I don’t want it
to use just the basic User Agent String. How would I go about giving my
script a User Agent string to use?

On Apr 10, 2008, at 07:56 AM, Ben A. wrote:

and all it gives me is:
SocketError: getaddrinfo: No address associated with nodename

Net::HTTP#initialize takes a host and a port, not URI and port:

$ ruby
require ‘net/http’
h = Net::HTTP.new ‘google.com’, ‘http’
resp, data = h.get ‘/’, “User-Agent” => “Ruby/#{RUBY_VERSION}”
p resp, data
#<Net::HTTPOK 200 OK readbody=true>
“[…]<input name=btnI type=submit value=“I’m Feeling Lucky”>[…]”
$

Eric H. wrote:

On Apr 10, 2008, at 07:56 AM, Ben A. wrote:

and all it gives me is:
SocketError: getaddrinfo: No address associated with nodename

Net::HTTP#initialize takes a host and a port, not URI and port:

$ ruby
require ‘net/http’
h = Net::HTTP.new ‘google.com’, ‘http’
resp, data = h.get ‘/’, “User-Agent” => “Ruby/#{RUBY_VERSION}”
p resp, data
#<Net::HTTPOK 200 OK readbody=true>
“[…]<input name=btnI type=submit value=“I’m Feeling Lucky”>[…]”
$

Putting that into irb gives me:
=> [#<Net::HTTPBadRequest 400 Bad Request readbody=true>, “\n\n400 Bad
Request\n\n

Bad Request

\nYour browser sent
a request that this server could not understand.

\nClient sent
malformed Host header

\n\n”]

I’ve had some luck with open-uri, but I still want to get the error
code’s from the site. I would really like to be able to create generic
HTTP requests, but it’s quite daunting.

Zoltan D. wrote:

Hi,

How about:

require ‘net/http’
user_agent = “you can set this part”
h = Net::HTTP.new(‘www.example.com’, 80)
resp, data = h.get(’/index.html’, {“User-Agent”=>user_agent})

Zaki

Tom A. wrote:

Hey guys,

I want my Ruby script to make a request to a server but I don’t want it
to use just the basic User Agent String. How would I go about giving my
script a User Agent string to use?

I’ve tried the above with the following:
user_agent = ‘Ruby Net::HTTP’
print “Getting h\n”
h = Net::HTTP.new(‘http://www.google.com’, 80)
print “getting response\n”
resp, data = h.get(’/’, {“User-Agent”=>user_agent})
print data.body
print resp
print “should’ve printed\n”

and all it gives me is:
SocketError: getaddrinfo: No address associated with nodename

method initialize in http.rb at line 560
method open in http.rb at line 560
method connect in http.rb at line 560
method timeout in timeout.rb at line 48
method timeout in timeout.rb at line 76
method connect in http.rb at line 560
method do_start in http.rb at line 553
method start in http.rb at line 542
method request in http.rb at line 1032
method get in http.rb at line 769
at top level in rubyheader.rb at line 12

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs