diff --git a/cookbook/014/01.rb b/cookbook/014/01.rb index b1fa3bb..74290ff 100644 --- a/cookbook/014/01.rb +++ b/cookbook/014/01.rb @@ -6,18 +6,17 @@ 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] +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] # ------------------------------------------------------------ require 'uri' puts 'using "net/http" with a URI' -puts 'request object:', \ - Net::HTTP.get(URI.parse("http://oreilly.com")) -puts 'full response:' +puts 'request object:' + \ + Net::HTTP.get(URI.parse("http://oreilly.com")).to_s response = Net::HTTP.get_response(URI.parse("http://oreilly.com/about/")) # ......... @@ -32,11 +31,11 @@ puts case response.code[0] else "Non-standard status code." end -puts 'Server=', response['Server'] -puts 'SERVER=', response['SERVER'] +puts 'Server=' + response['Server'] +puts 'SERVER=' + response['SERVER'] puts 'all keys:' -response.each_key { |key| puts key } +response.each_key { |key| puts " #{key}" } Net::HTTP.get_response('oreilly.com', '/about/') do |response| response.read_body do |segment| diff --git a/cookbook/014/02.rb b/cookbook/014/02.rb new file mode 100644 index 0000000..5f0ceea --- /dev/null +++ b/cookbook/014/02.rb @@ -0,0 +1,28 @@ +require 'net/http' +require 'uri' + +uri = URI.parse("https://www.donotcall.gov/") +puts 'uri=' + uri.to_s + +request = Net::HTTP.new(uri.host, uri.port) +# response = request.get('/') +# ^--- would have resulted in an error + +require 'net/https' +puts 'making a request with use_ssl=true and verify_mode=VERIFY_NONE' +request.use_ssl = true +request.verify_mode = OpenSSL::SSL::VERIFY_NONE +response = request.get('/') + +puts 'response.body.size=' + response.body.size.to_s + + +request = Net::HTTP.new(uri.host, uri.port) +puts 'making a request with use_ssl=true, ' + \ + 'ca_path=/etc/ssl/certs and verify_mode=VERIFY_PEER' +request.use_ssl = true +request.ca_path = "/etc/ssl/certs/" +request.verify_mode = OpenSSL::SSL::VERIFY_PEER +response = request.get('/') + +puts 'response.body.size=' + response.body.size.to_s