Handling cookies with Net::HTTP?

I am having a strange problem and it is partly due to my poor
understanding of cookies. So any help is greatly appreciated.

I am trying to navigate through a site which requires that I provide
them with the cookies they send me. I am having some trouble here. I
am grabbing the ‘set-cookie’ header they send back to me and storing
each cookie in a hash like: cookies[name] = value. Here is the code I
wrote for that:

def parse_cookie(set_cookie)
# Attempt to pull out SID, BID, CID
@cookies ||= {}
[‘SID’, ‘BID’, ‘CID’].each do |var|
cookie = set_cookie.scan(/(#{var}=.*?);/)
cookie = cookie.join(’’).split(’=’)
@cookies[cookie[0]] = cookie[1] unless cookie.size != 2
end
end

I only want the SID, BID, and CID cookies.

Then when I send another request to them I am doing the following:

def cookies
cookie_str = ‘’
@cookies.each do |k, v|
unless cookie_str == ‘’
cookie_str << ‘;’
end
cookie_str << k << ‘=’ << v
end unless @cookies.nil?
cookie_str
end

req.add_field ‘Cookie’, cookies #this calls the above method

Which ended up sending the following ‘Cookie’ header:

SID=VpI2zrrUjHDufXLqUec5VEWPia-JgWWQH3k-
DPOxEhOKcvtZh46eZxw2xrAwK5N645v3B4oRAtoI0b8_;BID=bWtbrEQbrPcZmyvFQoXsZoq
qWYrhV81w1698hx5-e1NUQZqE-tg9K9K36pt_CFIXcuW-
FOkplULieRehl4t5;CID=tSqTx-fWLck-ncBcWaZNUNo-XhTUc-
lM0xyHTe1VDmrZzVrMty0u3qSeAw4uTA6eW6Kl

Am I not sending the cookie header properly, because the site is not
accepting them. Do I need to include more information than just the
name and the value?

Thanks for your help.

Thank You,
Ben J.
E: [email protected]

On Sat, Jul 15, 2006 at 07:37:23AM +0900, Ben J. wrote:

I am having a strange problem and it is partly due to my poor
understanding of cookies. So any help is greatly appreciated.

How about using mechanize so you don’t have to worry about handling
cookies
and other things like redirects.

http://www.ntecs.de/blog/Blog/WWW-Mechanize.rdoc

Well, mechanize does a lot I do not need it to. I am looking for as
much speed as I can get and Net::HTTP seems to be quite a bit faster
without a lot of the excess features that I do not need. I just need
some help storing and sending cookies using Net::HTTP.

Thanks.

Thank You,
Ben J.
E: [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs