Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Fetch a URL in Ruby (See related posts)

require 'uri'
require 'net/http'
url = "http://www.whatever.com/whatever.txt"
r = Net::HTTP.get_response(URI.parse(url).host, URI.parse(url).path)


r.code = 200 | 404 | 500, etc
r.body = *text of page*

Comments on this post

jmoses posts on Jul 29, 2005 at 14:03
It's easier to do:

 r = Net::HTTP.get_reponse( URI.parse( url ) )


rather than parse twice and pull out the important bits.
jmoses posts on Jul 29, 2005 at 14:03
whups, I meant
get_response
, of course.
jicksta posts on Jun 06, 2007 at 22:59
Let's not forget open-uri guys. :)

require 'open-uri'
puts open("http://jicksta.com").read

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts