All of the code samples have now been consolidated and moved to my blog at http://house9.blogspot.com/. Many google searches still point here so I am leaving this blog operational.

Wednesday, January 6, 2010

Ruby Http Get with Net::HTTP


Resources



require 'net/http'
require 'uri'

def get_html_content(requested_url)
url = URI.parse(requested_url)
full_path = (url.query.blank?) ? url.path : "#{url.path}?#{url.query}"
the_request = Net::HTTP::Get.new(full_path)

the_response = Net::HTTP.start(url.host, url.port) { |http|
http.request(the_request)
}

raise "Response was not 200, response was #{the_response.code}" if the_response.code != "200"
return the_response.body
end

# this will fail with ArgumentError: HTTP request path is empty
s = get_html_content("http://www.google.com")
# these should be fine
s = get_html_content("http://www.google.com/")
s = get_html_content("http://github.com/search?q=http")
# above code does not handle redirects but raises exception for non-200
s = get_html_content("http://www.yahoo.com/") # http 302

2 comments:

doug said...

Thanks for this! I wove it into my app and looks to be working good.

Fernando Almeida said...

Thank you very much.

It works, but can be more simple:

require 'open-uri'
open('http://www.google.com').read

Custom Search
< ... back