Retrieve contents of URL as string

You can do the same without OpenURI: require ‘net/http’ require ‘uri’ def open(url) Net::HTTP.get(URI.parse(url)) end page_content = open(‘http://www.google.com’) puts page_content Or, more succinctly: Net::HTTP.get(URI.parse(‘http://www.google.com’))

Is there a workaround to open URLs containing underscores in Ruby?

This looks like a bug in URI, and uri-open, HTTParty and many other gems make use of URI.parse. Here’s a workaround: require ‘net/http’ require ‘open-uri’ def hopen(url) begin open(url) rescue URI::InvalidURIError host = url.match(“.+\:\/\/([^\/]+)”)[1] path = url.partition(host)[2] || “https://stackoverflow.com/” Net::HTTP.get host, path end end resp = hopen(“http://dear_raed.blogspot.com/2009_01_01_archive.html”)