nuby problem with net/http

K

kris

Hello

I am trying to do some basic http stuff with Ruby but I can't get the
following example from the Programming Ruby book to work.

Code from the book:

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]


I get this error:

c:/ruby/lib/ruby/1.8/net/protocol.rb:83:in `initialize': getaddrinfo:
no address associated with hostname. (SocketError)
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:83:in `new'
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:83:in
`connect'
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:82:in
`timeout'
from c:/program files/ruby/lib/ruby/1.8/timeout.rb:55:in `timeout'
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:82:in
`connect'
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:64:in
`initialize'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:430:in `open'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:430:in `do_start'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:419:in `start'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:821:in `request'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:618:in `get'
from http_msg.rb:4
Exit code: 1


Details:
Ruby: version 1.8.1 (2004-01-27)
OS: Win XP Pro
I'm behind a proxy with the only information I have about it are IP
address and port number.

Many thanks for your help!

K
 
D

Daniel Bovensiepen

Hello kris

Hello

I am trying to do some basic http stuff with Ruby but I can't get the
following example from the Programming Ruby book to work.

Code from the book:

require 'net/http'

h = Net::HTTP.new('www.pragmaticprogrammer.com', 80)

maybe yout try here
h = Net::HTTP.new('www.pragmaticprogrammer.com',80,'p-addr', 'p-port')
p-addr: the addresss of your proxy
p-port: the port of your proxy
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]


I get this error:

c:/ruby/lib/ruby/1.8/net/protocol.rb:83:in `initialize': getaddrinfo:
no address associated with hostname. (SocketError)
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:83:in `new'
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:83:in
`connect'
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:82:in
`timeout'
from c:/program files/ruby/lib/ruby/1.8/timeout.rb:55:in `timeout'
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:82:in
`connect'
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:64:in
`initialize'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:430:in `open'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:430:in `do_start'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:419:in `start'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:821:in `request'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:618:in `get'
from http_msg.rb:4
Exit code: 1


Details:
Ruby: version 1.8.1 (2004-01-27)
OS: Win XP Pro
I'm behind a proxy with the only information I have about it are IP
address and port number.

Many thanks for your help!

K


bovi
 
P

Phil Tomson

Hello

I am trying to do some basic http stuff with Ruby but I can't get the
following example from the Programming Ruby book to work.

Code from the book:

Note that a couple of things have changed in Net::HTTP since the book came
out, see changes below:
require 'net/http'

h = Net::HTTP.new('www.pragmaticprogrammer.com', 80)
resp, data = h.get('/index.html', nil )

HTTP#get now only returns a single Net::HTTPResponse object, but don't
worry, you can still get at the data (Or body of the page) by doing:

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


I get this error:

c:/ruby/lib/ruby/1.8/net/protocol.rb:83:in `initialize': getaddrinfo:
no address associated with hostname. (SocketError)
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:83:in `new'
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:83:in
`connect'
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:82:in
`timeout'
from c:/program files/ruby/lib/ruby/1.8/timeout.rb:55:in `timeout'
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:82:in
`connect'
from c:/program files/ruby/lib/ruby/1.8/net/protocol.rb:64:in
`initialize'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:430:in `open'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:430:in `do_start'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:419:in `start'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:821:in `request'
from c:/program files/ruby/lib/ruby/1.8/net/http.rb:618:in `get'
from http_msg.rb:4
Exit code: 1


Details:
Ruby: version 1.8.1 (2004-01-27)
OS: Win XP Pro
I'm behind a proxy with the only information I have about it are IP
address and port number.


It's not working because you're behind a firewall. You need to use a
proxy like so:

require 'net/http'
proxy_addr = 'your.proxy.host'
proxy_port = 8080
:
Net::HTTP::proxy(proxy_addr, proxy_port).start('www.example.com')
{|http|
# always connect to your.proxy.addr:8080
:
}


Phil
 
K

kris

Just want to thank you both for your quick responses...it works great now!

thanks again
K
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top