Proxying is when HTTP requests do not go directly between the client and the HTTP server, but through a third party en route. In some situations it might be necessary to use an HTTP proxy for your HTTP requests. This is a common scenario in schools and offices where Web access is regulated or filtered.
net/http supports proxying by creating an HTTP proxy class upon which you can then use and perform the regular HTTP methods. To create the proxy class, use Net::HTTP::Proxy. For example:

web_proxy = Net::HTTP::Proxy('your.proxy.hostname.or.ip', 8080)

This call to Net::HTTP::Proxy generates an HTTP proxy class that uses a proxy with a particular hostname on port 8080. You would use such a proxy in this fashion:

require 'net/http'
web_proxy = Net::HTTP::Proxy('your.proxy.hostname.or.ip', 8080)
url = URI.parse('http://www.rubyinside.com/test.txt')
web_proxy.start(url.host, url.port) do |http|
  req = Net::HTTP::Get.new(url.path)
  puts http.request(req).body
end
Advertisements