Register now and start sharing your code snippets.
-->
Example of how to fetch a URL with Net:HTTP and Ruby
Ruby posted 7 months ago by christian
1 require 'net/http' 2 require 'net/https' 3 4 url = URI.parse('http://www.google.com/yo?query=yahoo') 5 6 http = Net::HTTP.new(url.host, url.port) 7 8 http.open_timeout = http.read_timeout = 10 # Set open and read timeout to 10 seconds 9 http.use_ssl = (url.scheme == "https") 10 11 headers = { 12 'User-Agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12', 13 'If-Modified-Since' => '', 14 'If-None-Match' => '' 15 } 16 17 # Note to self, use request_uri not path: http://www.ruby-doc.org/core/classes/URI/HTTP.html#M004934 18 response, body = http.get(url.request_uri, headers) 19 20 puts response.code 21 puts response.message 22 23 response.each {|key, val| puts key + ' = ' + val}