Automatic HTTP Basic Authentication In Ruby on Rails

Well, you may want to use HTTP basic auth mechanism of Net::HTTP library. Basically this is how i would do it:

require 'net/http'

req = Net::HTTP::Post.new(url.path) req.basic_auth("username","password") req.body = xml_query # contents the raw query req.content_type = "application/x-www-form-urlencoded" req.content_length = xml_query.length

    result = nil     begin       timeout(3) do         _result = Net::HTTP.new(url.host,url.port).start { |http| http.request(req)}         result = _result.body       end     rescue Timeout::Error       result = nil     rescue       result = nil     end

But probably you want a much simpler solution, another solution would be to use SimpleHttp library.

str = SimpleHttp.get URI.parse("http://username:password@www.example.com")

URL:

http://simplehttp.rubyforge.org/