box-o-sand/oldstuff/RubyFun/cookbook/014/01.rb

45 lines
1.4 KiB
Ruby
Raw Normal View History

require 'open-uri'
2011-07-26 01:32:26 +00:00
puts 'using "open-uri"'
puts open('http://oreilly.com/').read(200)
# ------------------------------------------------------------
require 'net/http'
puts 'using "net/http"'
response = Net::HTTP.get_response('oreilly.com', '/about/')
puts 'response.code=' + response.code.to_s
puts 'response.body.size=' + response.body.size.to_s
puts 'response[\'Content-type\']=' + response['Content-type']
puts 'response[0, 200]' + response.body[0, 200]
2011-07-26 01:32:26 +00:00
# ------------------------------------------------------------
require 'uri'
puts 'using "net/http" with a URI'
puts 'request object:' + \
Net::HTTP.get(URI.parse("http://oreilly.com")).to_s
2011-07-26 01:32:26 +00:00
response = Net::HTTP.get_response(URI.parse("http://oreilly.com/about/"))
# .........
puts "Success!" if response.is_a? Net::HTTPOK
puts case response.code[0]
when ?1 then "Status code indicates an HTTP informational response."
when ?2 then "Status code indicates success."
when ?3 then "Status code indicates redirection."
when ?4 then "Status code indicates client error."
when ?5 then "Status code indicates server error."
else "Non-standard status code."
end
puts 'Server=' + response['Server']
puts 'SERVER=' + response['SERVER']
2011-07-26 01:32:26 +00:00
puts 'all keys:'
response.each_key { |key| puts " #{key}" }
2011-07-26 01:32:26 +00:00
Net::HTTP.get_response('oreilly.com', '/about/') do |response|
response.read_body do |segment|
puts "Received segment of #{segment.size} byte(s)!"
end
end