Topic: Net::HTTP:Request setting the cookies

I'm working on an app that performs a pseudo proxy function. I would like to take cookies previously retrieved from a response and add them to a new request. Thanks for taking a look.

ie

    http = Net::HTTP.new('www.facebook.com', 80)
    #http.use_ssl = true
    resp = nil
    cookie = String.new

    http.start do |http|

      request = Net::HTTP::Post.new('/')
      request['referer'] = 'http://www.example.com/'
      request['user-agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008051206 Firefox/3.0'
      resp = http.request(request)
      cookie = resp['Set-Cookie']
     
    end

    http = Net::HTTP.new('www.samplesite.com', 443)
    http.use_ssl = true
   
    http.start do |http|

      request = Net::HTTP::Post.new('/login.php')
     
      ######
      #Here is where I need an example
      ######

      request['Cookie'] = ?????

      ######
   

      request['user-agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008051206 Firefox/3.0'
      resp = http.request(request)
     
      resp.value
     
    end

Re: Net::HTTP:Request setting the cookies

its just a string, as a normal set of cookies would be so here's what I did

  custom_request['cookie'] = "";
   
  resp.get_fields('set-cookie').each do |cookie|
   
    custom_request['cookie'] = custom_request['cookie'] + cookie.split("\; ")[0] + "\; "

  end


I was getting frustrated with the << operator on request['cookie'] as it wasn't working for some reason (I'm relatively new to ruby). And as a result you end up with the not so pretty result above.

Any suggestions how I can shorten this up?

Last edited by nousplacidus (2008-08-31 15:02:47)