How to maintain a session?

237 views
Skip to first unread message

thad

unread,
Jan 12, 2014, 6:32:15 PM1/12/14
to typh...@googlegroups.com
Sorry, but I'm really new and am lost as to how to maintain a session with a website. The code I'm using is 

login_url_string = "or-authenticate.php"
login_params = "username=#{URI.escape(username, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&password=#{URI.escape(password, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"
login_request = Typhoeus::Request.new("#{site_url}#{login_url_string}",
    :method => :post,
    :headers => {"Content-Type" => "application/x-www-form-urlencoded"},
    :body => login_params)
login_request.on_complete do |response|
# *** LOOK UP LEVEL ***
  fromrange = 1389502800
  torange = 1389589199
  group = 9
  choose_floor_urlstring = "modules/dayviewAJAX.php"
  choose_floor_params = "fromrange=#{fromrange}&torange=#{torange}&group=#{group}&ajax_indicator=FALSE"
  @choose_floor_request = Typhoeus::Request.new("#{site_url}#{choose_floor_urlstring}",
      :method => :post,
      :headers => {"Content-Type" => "application/x-www-form-urlencoded"},
      :body => choose_floor_params)
  hydra.queue(@choose_floor_request)
end
hydra.queue(login_request)
hydra.run

When I run this and get back the information the first request runs fine. But the second says that I'm not logged in. Any help would be greatly appreciated.

Denis Lamotte

unread,
Jan 13, 2014, 3:19:48 AM1/13/14
to typh...@googlegroups.com
From the doc

Cookies

Typhoeus::Request.get("www.example.com", cookiefile: "/path/to/file", cookiejar: "/path/to/file")

Here, cookiefile is a file to read cookies to send from, and cookiejar is a file to write received cookies to. So, if you just want cookies enabled, you need to pass in a same filename in both options.


Hope it help :-)

Hans Hasselberg

unread,
Jan 13, 2014, 3:49:26 AM1/13/14
to typh...@googlegroups.com

Hello,

Thank you Denis for jumping in! Your solution is the most elegant one! 
Unfortunately people were having trouble with the cookiejar and cookiefile options. If you're one of these Thad then you're better off with looking into the headers on your own and to pass the cookies manually.
Another thing I noticed in your example is that you encode the body yourself. That's not necessary, you can pass a hash to body and Typhoeus will take care of that.

--
You received this message because you are subscribed to the Google Groups "Typhoeus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to typhoeus+u...@googlegroups.com.
To post to this group, send email to typh...@googlegroups.com.
Visit this group at http://groups.google.com/group/typhoeus.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages