# Net::HTTP Logging in / Persistent sessions problem

**URL:** <https://www.ruby-forum.com/t/net-http-logging-in-persistent-sessions-problem/105451>\
**Category:** Ruby\
**Created:** [May 30, 2007, 4:25am UTC](https://www.ruby-forum.com/t/net-http-logging-in-persistent-sessions-problem/105451 "2007-05-30T04:25:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yeago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eada6e/32.png) [@yeago](https://www.ruby-forum.com/u/yeago)\
**Post date:** [May 30, 2007, 4:25am UTC](https://www.ruby-forum.com/t/net-http-logging-in-persistent-sessions-problem/105451/1 "2007-05-30T04:25:23Z")

</div>

I am able to login to a website at [authenticate.domain.com](http://authenticate.domain.com) but when I  
attempt to visit [panel.domain.com](http://panel.domain.com) it appears the http session has been  
lost.

Code:  
Net::HTTP.start(‘[authenticate.domain.com](http://authenticate.domain.com)’,80){|http|  
log = http.post(‘script.php’,params)  
puts log.body  
}

```
            Net::HTTP.start('panel.domain.com',80){|http|
                    puts http.get('/index.cfm').body
            }

```

* * *

So, what could be the problem? Either the second Net::HTTP.start call is  
beginning a new session or the first call is forgetting a vital cookie.  
Anyone? Thanks!

---

<div class="post-metadata">

**Author:** ![yeago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eada6e/32.png) [@yeago](https://www.ruby-forum.com/u/yeago)\
**Post date:** [May 30, 2007, 10:32am UTC](https://www.ruby-forum.com/t/net-http-logging-in-persistent-sessions-problem/105451/2 "2007-05-30T10:32:46Z")

</div>

On 30 May 2007, at 04:25, Sy Ys wrote:

> ```
> Net::HTTP.start('panel.domain.com',80){|http|
> puts http.get('/index.cfm').body
> }
> 
> ```
> 
> * * *
> 
> So, what could be the problem? Either the second Net::HTTP.start  
> call is  
> beginning a new session or the first call is forgetting a vital  
> cookie.  
> Anyone? Thanks!

In your code you are basically getting the body of the response only.  
Session or cookie information should be in the header… Basically in  
a Net::HTTPResponse object you have the Net::HTTPHeader mixed in. Try  
looking at the both api’s and you’ll see it clearly…

Cheers,

Enrique

---

<div class="post-metadata">

**Author:** ![yeago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eada6e/32.png) [@yeago](https://www.ruby-forum.com/u/yeago)\
**Post date:** [May 31, 2007, 1:12am UTC](https://www.ruby-forum.com/t/net-http-logging-in-persistent-sessions-problem/105451/3 "2007-05-31T01:12:48Z")

</div>

Enrique Comba R. wrote:

> In your code you are basically getting the body of the response only.  
> Session or cookie information should be in the header… Basically in  
> a Net::HTTPResponse object you have the Net::HTTPHeader mixed in. Try  
> looking at the both api’s and you’ll see it clearly…
> 
> Cheers,
> 
> Enrique

Ok, great. After doing some studying I understand a bit more now. Thank  
you!

I’ve got my script logging in but the session still doesn’t persist. I  
auto-follow a few redirects and I end up back where I was (not logged  
in).

I did notice Net::HTTP’s get\_fields(‘set-cookie’).

Do I need to use set\_content\_type(‘set-cookie’,@@cookie)? where @@cookie  
= get\_fields(‘set-cookie’) # of the first login attempt?

---

<div class="post-metadata">

**Author:** ![yeago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eada6e/32.png) [@yeago](https://www.ruby-forum.com/u/yeago)\
**Post date:** [May 31, 2007, 1:14am UTC](https://www.ruby-forum.com/t/net-http-logging-in-persistent-sessions-problem/105451/4 "2007-05-31T01:14:09Z")

</div>

Aaron P. wrote:

> Net::HTTP doesn’t save your cookies. If you’d like to have your cookies  
> automatically saved between each request, check out WWW::Mechanize.
> 
> [http://mechanize.rubyforge.org/](http://mechanize.rubyforge.org/)

Oh. Then what does get\_fields(‘set-cookie’) do?

---

<div class="post-metadata">

**Author:** ![yeago](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eada6e/32.png) [@yeago](https://www.ruby-forum.com/u/yeago)\
**Post date:** [May 31, 2007, 1:12am UTC](https://www.ruby-forum.com/t/net-http-logging-in-persistent-sessions-problem/105451/5 "2007-05-31T01:12:49Z")

</div>

On Wed, May 30, 2007 at 11:25:26AM +0900, Sy Ys wrote:

> ```
> Net::HTTP.start('panel.domain.com',80){|http|
> puts http.get('/index.cfm').body
> }
> 
> ```
> 
> * * *
> 
> So, what could be the problem? Either the second Net::HTTP.start call is  
> beginning a new session or the first call is forgetting a vital cookie.  
> Anyone? Thanks!

Net::HTTP doesn’t save your cookies. If you’d like to have your cookies  
automatically saved between each request, check out WWW::Mechanize.

[http://mechanize.rubyforge.org/](http://mechanize.rubyforge.org/)
