Net::HTTP persistent among requests

Hi there,

I am trying to use ruby to login to a web site and then
browse some of the other web pages.
As shown below is my codes.
I can pass the login phase,
but I can not access other pages after login.
I only got the login page instead.

Could someone help?




require ‘uri’
require ‘net/http’
require ‘net/https’

url = “
uri = URI(url)

Net::HTTP.start(, uri.port) do |http|
request =
request.set_form_data(‘email’ => ‘[email protected]’, ‘pass’ =>
response = http.request request

url = “
uri = URI(url)
request = uri
response = http.request request
print response.body

I think you need to capture the cookie and resend it every time you
request a new page. Is your application supporting custom password
headers? Maybe you can use those for authentication instead of the
session cookie.


Thanks for your reply.
But how do I capture and resend it?

The ‘response’ object contains the cookies.

Put this after the successful request:

puts response[“set-cookie”]

(all header reference must be lowercase, like the ‘set-cookie’ here)

or list all headers:

response.each { |e| puts e }

Thanks for the help.
It worked after I modified the code as shown below:

req =
req[‘Cookie’] = cookie
resp = http.request(req)