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
Most Viewed Articles (in Ruby ) |
Latest Articles (in Ruby) |
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Archived Comments
1. Thanks, I'm writing a crawler in ruby :).
View Tutorial By: Alex at 2012-02-02 10:07:10
2. Hi
Where puts this code in ror app
View Tutorial By: martin at 2013-01-31 08:51:12