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 Arra 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?
--
Posted via http://www.ruby-forum.com/\.
Zoltan Dezso 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 Arra 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
···
--
Posted via http://www.ruby-forum.com/\.
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\">[...]"
$
···
On Apr 10, 2008, at 07:56 AM, Ben Aroia wrote:
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
Eric Hodel 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>, "<!DOCTYPE
HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<HTML><HEAD>\n<TITLE>400 Bad
Request</TITLE>\n</HEAD><BODY>\n<H1>Bad Request</H1>\nYour browser sent
a request that this server could not understand.<P>\nClient sent
malformed Host header<P>\n</BODY></HTML>\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.
···
On Apr 10, 2008, at 07:56 AM, Ben Aroia wrote:
--
Posted via http://www.ruby-forum.com/\.