How to use access url using Ruby

S

sujeet kumar

hi
I am new to Ruby. I want to access http address using Ruby program .
I run the sample code of book The Pragmatic Programmer's Guide but it
doesn't work.
code is:
require 'net/http'

h =3D Net::HTTP.new('www.pragmaticprogrammer.com', 80)
resp, data =3D h.get('/index.html', nil )
puts "Code =3D #{resp.code}"
puts "Message =3D #{resp.message}"
resp.each {|key, val| printf "%-14s =3D %-40.40s\n", key, val }
p data[0..55]

Error is:
c:/ruby/lib/ruby/1.8/net/protocol.rb:83:in `initialize': getaddrinfo: no ad=
dress
associated with hostname. (SocketError)
from c:/ruby/lib/ruby/1.8/net/protocol.rb:83:in `new'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:83:in `connect'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:82:in `timeout'
from c:/ruby/lib/ruby/1.8/timeout.rb:55:in `timeout'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:82:in `connect'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:64:in `initialize'
from c:/ruby/lib/ruby/1.8/net/http.rb:430:in `open'
from c:/ruby/lib/ruby/1.8/net/http.rb:430:in `do_start'
from c:/ruby/lib/ruby/1.8/net/http.rb:419:in `start'
from c:/ruby/lib/ruby/1.8/net/http.rb:821:in `request'
from c:/ruby/lib/ruby/1.8/net/http.rb:615:in `get'
from url.rb:4

For information , I access internet through proxy server . Can anybody
tell me the error?
thanks

sujeet
 
R

Robert Klemme

sujeet said:
hi
I am new to Ruby. I want to access http address using Ruby program .
I run the sample code of book The Pragmatic Programmer's Guide but it
doesn't work.
code is:
require 'net/http'

h = Net::HTTP.new('www.pragmaticprogrammer.com', 80)
resp, data = h.get('/index.html', nil )
puts "Code = #{resp.code}"
puts "Message = #{resp.message}"
resp.each {|key, val| printf "%-14s = %-40.40s\n", key, val }
p data[0..55]

Error is:
c:/ruby/lib/ruby/1.8/net/protocol.rb:83:in `initialize': getaddrinfo:
no address associated with hostname. (SocketError)
from c:/ruby/lib/ruby/1.8/net/protocol.rb:83:in `new'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:83:in `connect'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:82:in `timeout'
from c:/ruby/lib/ruby/1.8/timeout.rb:55:in `timeout'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:82:in `connect'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:64:in `initialize'
from c:/ruby/lib/ruby/1.8/net/http.rb:430:in `open'
from c:/ruby/lib/ruby/1.8/net/http.rb:430:in `do_start'
from c:/ruby/lib/ruby/1.8/net/http.rb:419:in `start'
from c:/ruby/lib/ruby/1.8/net/http.rb:821:in `request'
from c:/ruby/lib/ruby/1.8/net/http.rb:615:in `get'
from url.rb:4

For information , I access internet through proxy server . Can anybody
tell me the error?
thanks

See "Accessing via Proxy" on
http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html

Also preferrable use the block form to ensure proper closing of socket
connections.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top