Net::HTTP Cookies / Headers (almost there...)

Discussion in 'Ruby' started by Sy Ys, Jun 7, 2007.

  1. Sy Ys

    Sy Ys Guest

    I'm attempting to login and follow redirects, however, the below script
    doesn't seem to do it. I arrive back at the main login page. Am I doing
    something obviously wrong? Am I passing bad headers or wrong cookies?
    See comments below.

    Also, please don't tell me to use Mechanize. I'm trying to improve the
    documentation of this library.

    Code ------------------
    domain = 'login.domain.com'
    script = '/login.cfm'
    params = 'email=&password=passw4rd'
    Net::HTTP.start(domain,80){|http|
    loginRsp = http.post2(script,params)
    loginRsp.each {|key, val| puts key + ' = ' + val}
    puts "FIRST RESPONSE===========\r"
    puts loginRsp['location'] #Seems ok => Login2.domain.com
    request = Net::HTTP::Get.new(loginRsp['location'])
    headers = {'Cookie' => loginRsp.response['set-cookie']}
    firstRsp = http.request(request,headers)
    puts "SECOND RESPONSE===========\r"
    puts firstRsp['location'] #Might be ok. =>
    Login.domain.com
    request = Net::HTTP::Get.new(firstRsp['location'])
    headers = {'Cookie' => firstRsp.response['set-cookie']}
    secondRsp = http.request(request,headers)
    puts "THIRD RESPONSE===========\r"
    puts secondRsp['location'] #Not right. Should be
    home.domain.com but it is simply domain.com.
    request = Net::HTTP::Get.new(secondRsp['location'])
    headers = {'Cookie' => secondRsp.response['set-cookie']}
    thirdRsp = http.request(request,headers)
    puts "=======================END\r"
    }

    --
    Posted via http://www.ruby-forum.com/.
     
    Sy Ys, Jun 7, 2007
    #1
    1. Advertising

  2. On Thu, Jun 07, 2007 at 12:42:45PM +0900, Sy Ys wrote:
    > I'm attempting to login and follow redirects, however, the below script
    > doesn't seem to do it. I arrive back at the main login page. Am I doing
    > something obviously wrong? Am I passing bad headers or wrong cookies?
    > See comments below.
    >
    > Also, please don't tell me to use Mechanize. I'm trying to improve the
    > documentation of this library.


    Which library?

    >
    > Code ------------------
    > domain = 'login.domain.com'
    > script = '/login.cfm'
    > params = 'email=&password=passw4rd'
    > Net::HTTP.start(domain,80){|http|
    > loginRsp = http.post2(script,params)
    > loginRsp.each {|key, val| puts key + ' = ' + val}
    > puts "FIRST RESPONSE===========\r"
    > puts loginRsp['location'] #Seems ok => Login2.domain.com
    > request = Net::HTTP::Get.new(loginRsp['location'])
    > headers = {'Cookie' => loginRsp.response['set-cookie']}
    > firstRsp = http.request(request,headers)
    > puts "SECOND RESPONSE===========\r"
    > puts firstRsp['location'] #Might be ok. =>
    > Login.domain.com
    > request = Net::HTTP::Get.new(firstRsp['location'])
    > headers = {'Cookie' => firstRsp.response['set-cookie']}
    > secondRsp = http.request(request,headers)
    > puts "THIRD RESPONSE===========\r"
    > puts secondRsp['location'] #Not right. Should be
    > home.domain.com but it is simply domain.com.
    > request = Net::HTTP::Get.new(secondRsp['location'])
    > headers = {'Cookie' => secondRsp.response['set-cookie']}
    > thirdRsp = http.request(request,headers)
    > puts "=======================END\r"
    > }


    There are a couple things wrong. First, you are not parsing the cookie
    header. You are not supposed to send back the same data you received.
    See RFC 2109, and 2965.

    Second, you keep reassigning your headers hash. This means that even if
    you were sending back a properly formatted cookie header, you would only
    be sending back the last cookie you received! The server could be
    trying to set multiple cookies on your client, and expects that all of
    those cookies be sent back.

    Hope the helps!

    --
    Aaron Patterson
    http://tenderlovemaking.com/
     
    Aaron Patterson, Jun 7, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antony

    cookies in HTTP POST headers

    Antony, Feb 1, 2005, in forum: HTML
    Replies:
    1
    Views:
    543
    Toby Inkster
    Feb 2, 2005
  2. Bart Nessux
    Replies:
    3
    Views:
    303
    =?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?=
    Jul 3, 2004
  3. Replies:
    4
    Views:
    1,066
    Gibble
    May 10, 2007
  4. _Who
    Replies:
    7
    Views:
    2,683
  5. Ian
    Replies:
    2
    Views:
    1,959
Loading...

Share This Page