Reading URL content using Ruby (HTTP)
By: Emiley J.
Example 1: Simple GET+print
require 'net/http'
Net::HTTP.get_print 'www.example.com', '/index.html'
Example 2: Simple GET+print by URL
require 'net/http'
require 'uri'
Net::HTTP.get_print URI.parse('http://www.example.com/index.html')
Example 3: More generic GET+print
require 'net/http'
require 'uri'
url = URI.parse('http://www.example.com/index.html')
res = Net::HTTP.start(url.host, url.port) {|http|
http.get('/index.html')
}
puts res.body
Example 4: More generic GET+print
require 'net/http'
url = URI.parse('http://www.example.com/index.html')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
puts res.body
Archived Comments
1. Hi
Where puts this code in ror app ? how to enable ?
View Tutorial By: martin at 2013-01-31 08:51:12
2. Thanks, I'm writing a crawler in ruby :).
View Tutorial By: Alex at 2012-02-02 10:07:10
Most Viewed Articles (in Ruby ) |
Latest Articles (in Ruby) |
Comment on this tutorial