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?

Regards,

Kevin

#!/usr/bin/ruby

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

url = “http://www.mycompany.com/signin.php
uri = URI(url)

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

url = “http://www.snsbay.com/control/order.php
uri = URI(url)
request = Net::HTTP::Get.new uri
response = http.request request
print response.body
end

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.

Cheers

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 = Net::HTTP::Get.new(uri)
req[‘Cookie’] = cookie
resp = http.request(req)