46 lines
1.4 KiB
Ruby
46 lines
1.4 KiB
Ruby
require 'open-uri'
|
|
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
|
|
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
|