Skip to content

Instantly share code, notes, and snippets.

@kares
Created November 13, 2011 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kares/1361989 to your computer and use it in GitHub Desktop.
Save kares/1361989 to your computer and use it in GitHub Desktop.
GET a content of a https URL (handling redirects)
require "uri"
require "net/https"
location = "https://..." # the URL to GET
begin
uri = URI.parse(location)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
case response = http.request(request)
when Net::HTTPRedirection then location = response['location']
when Net::HTTPClientError, Net::HTTPServerError then response.error!
end
end until Net::HTTPSuccess === response
puts response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment