The blog has moved to ... Many google searches point here so I am leaving it operational, but there will be no new posts.

Wednesday, January 6, 2010

Ruby Http Get with Net::HTTP


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 =

the_response = Net::HTTP.start(, url.port) { |http|

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

# this will fail with ArgumentError: HTTP request path is empty
s = get_html_content("")
# these should be fine
s = get_html_content("")
s = get_html_content("")
# above code does not handle redirects but raises exception for non-200
s = get_html_content("") # http 302

No comments: