Automatic HTTP Basic Authentication In Ruby on Rails

Hi,

i need to access a url
www.sometesturl3456.com/index.php

It prompts HTTP Authentication window asking username and password. I
have do automatic HTTP Basic authentication without manual entry of
username and password, as i am reading the content of page in ruby in
rails using:

response = open(“www.sometesturl3456.com/index.php”).read

Please help me to know how this can be done.

Thanks
Kiran

On 3/26/07, Kiran [email protected] wrote:

response = open(“www.sometesturl3456.com/index.php”).read

Please help me to know how this can be done.

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:[email protected]”)

URL:

http://simplehttp.rubyforge.org/


gnufied

There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.