As well as basic document retrieval, net/http supports the Basic Authentication scheme used by many Web servers to protect their documents behind a password-protected area.This demonstration shows how the flexibility of performing the entire request with Net::HTTP.start can come in useful:

require 'net/http'
url = URI.parse('http://www.rubyinside.com/test.txt')
Net::HTTP.start(url.host, url.port) do |http|
  req = Net::HTTP::Get.new(url.path)
  req.basic_auth('username', 'password')
  puts http.request(req).body
end
Advertisements