done with 14.1

This commit is contained in:
Dan Buch 2011-07-25 21:32:26 -04:00
parent 2bb5d6d954
commit d92413c2b1

View File

@ -1,3 +1,45 @@
require 'open-uri' require 'open-uri'
head = open('http://www.oreilly.com/').read(200) puts 'using "open-uri"'
puts head 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
puts 'response.body.size=', response.body.size
puts 'response[\'Content-type\']=', response['Content-type']
puts 'response[0, 200]', response.body[0, 200]
# ------------------------------------------------------------
require 'uri'
puts 'using "net/http" with a URI'
puts 'request object:', \
Net::HTTP.get(URI.parse("http://oreilly.com"))
puts 'full response:'
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']
puts 'all keys:'
response.each_key { |key| puts key }
Net::HTTP.get_response('oreilly.com', '/about/') do |response|
response.read_body do |segment|
puts "Received segment of #{segment.size} byte(s)!"
end
end