working through 14.3
This commit is contained in:
parent
963e423d03
commit
ea74004d4e
64
cookbook/014/03.rb
Normal file
64
cookbook/014/03.rb
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
require 'net/http'
|
||||||
|
require 'uri'
|
||||||
|
|
||||||
|
|
||||||
|
# A simple wrapper method that accepts either strings or URI objects
|
||||||
|
# and performs an HTTP GET.
|
||||||
|
|
||||||
|
module Net
|
||||||
|
class HTTP
|
||||||
|
def HTTP.get_with_headers(uri, headers=nil)
|
||||||
|
uri = URI.parse(uri) if uri.respond_to? :to_str
|
||||||
|
start(uri.host, uri.port) do |http|
|
||||||
|
return http.get(uri.path, headers)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Let's get a web page in German.
|
||||||
|
|
||||||
|
res = Net::HTTP.get_with_headers('http://www.google.com/',
|
||||||
|
{'Accept-Language' => 'de'})
|
||||||
|
|
||||||
|
# Check a bit of the body to make sure it's really in German
|
||||||
|
s = res.body.size
|
||||||
|
puts 'part of body in german=' + res.body[s-3670..s-3470].to_s
|
||||||
|
|
||||||
|
|
||||||
|
Net::HTTP.get_with_headers('http://www.google.com/',
|
||||||
|
{'User-Agent' => 'Ruby Web Browser v1.0'})
|
||||||
|
|
||||||
|
|
||||||
|
uncompressed = Net::HTTP.get_with_headers('http://www.cnn.com/')
|
||||||
|
puts 'uncompressed body size=' + uncompressed.body.size.to_s
|
||||||
|
|
||||||
|
gzipped = Net::HTTP.get_with_headers('http://www.cnn.com/',
|
||||||
|
{'Accept-Encoding' => 'gzip'})
|
||||||
|
puts 'gzipped Content-Encoding=' + gzipped['Content-Encoding']
|
||||||
|
puts 'gzipped body size=' + gzipped.body.size.to_s
|
||||||
|
|
||||||
|
require 'zlib'
|
||||||
|
require 'stringio'
|
||||||
|
|
||||||
|
body_io = StringIO.new(gzipped.body)
|
||||||
|
unzipped_body = Zlib::GzipReader.new(body_io).read()
|
||||||
|
|
||||||
|
puts 'unzipped body size=' + unzipped_body.size.to_s
|
||||||
|
|
||||||
|
|
||||||
|
uri = URI.parse('http://www.google.com/')
|
||||||
|
request = Net::HTTP::Get.new(uri.path)
|
||||||
|
['en_us', 'en', 'en_gb', 'ja'].each do |language|
|
||||||
|
request.add_field('Accept-Language', language)
|
||||||
|
end
|
||||||
|
puts 'request[\'Accept-Language\']=' + request['Accept-Language'].to_s
|
||||||
|
|
||||||
|
Net::HTTP.start(uri.host, uri.port) do |http|
|
||||||
|
response = http.request(request)
|
||||||
|
puts 'response[\'Content-Type\']=' + response['Content-Type']
|
||||||
|
puts 'response headers:'
|
||||||
|
response.each_key do |key|
|
||||||
|
puts " #{key}"
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user