An example shows how to use Ruby write an HTTP client.
require 'net/http'
def fetch(loc, port, url)
z=nil
h=Net::HTTP.new(loc,port)
begin
resp, data = h.get(url,z)
puts "Code = #{resp.code} #{resp.message}"
resp.each {|key, val| printf "%-14s = %-40.40s\n", key, val}
p data
rescue Net::ProtoFatalError => detail
head = detail.data
head.each {|key,val| puts "#{key} #{val}"}
if head.code=="404"
puts "File not Found"
elsif head.code=="401"
print "Authorization Required\n User name?"
gets
uname=$_.chomp
print "Password?"
gets
pwd=$_.chomp
unpwd=uname + ":" + pwd
puts unpwd
hval=[unpwd].pack("m")
z={'Authorization' => 'Basic ' + hval}
retry
end
rescue SystemCallError
puts "Can't connect"
end
end
fetch('www.blogjava.net', 80, '/blake/articles/5471.html')
Technorati Tags: Ruby | ROR