Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

Can't figure out how to set a cookie

I'm doing something wrong here, but I can't see what it is.  I'm trying to set a cookie value in the cookie_jar but nothing happens:

I'm sure it's a syntax error...

Here is the code:
        use LWP;
        use HTTP::COOKIES;
         my($browser) = LWP::UserAgent->new;
        $browser->cookie_jar(HTTP::Cookies->new());
        $browser->cookie_jar->set_cookie   ('1','ASP.NET_SessionId',$GLASP_NET_SessionId,"/","","",1,1,900,0);

When I run Dumper on the $browser the cookie jar is always empty.

$VAR1 = bless( {
                 'COOKIES' => {}
               }, 'HTTP::Cookies' );
0
geekdad1
Asked:
geekdad1
  • 3
  • 2
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
This page makes it look like you have things out of order.

http://search.cpan.org/~gaas/HTTP-Cookies-6.01/lib/HTTP/Cookies.pm
0
 
geekdad1Author Commented:
I've been through that page and others with no luck.  Not sure what you mean by out of order.  You have to set up a user agent first.  You have to have a cookie jar in order to use the set-cookie method.  What could possibly be out of order?

If you have an example that works, it would go a long way to helping me out here.
0
 
Dave BaldwinFixer of ProblemsCommented:
According to that page I linked, you have to set up the cookie jar first before you can assign anything to it.
0
 
geekdad1Author Commented:
OK:

This code also doesn't work.

       use LWP;
        use HTTP::COOKIES;
         my($browser) = LWP::UserAgent->new;
        $cjar =HTTP::Cookies->new();
        $cjar->cookie_jar->set_cookie   ('1','ASP.NET_SessionId',$GLASP_NET_SessionId,"/","","",1,1,900,0);

I ran cpan to do an upgrade but it said I had the most recent version.  For some reason my system has several copies of Cookies.pm in various locations.  I got rid of all but the latest to make sure there wasn't any compatibility problems.

Not to worry, I figured it out by debugging the Cookies.pm code.  Turns out that if you don't include a value for port, it gives up and returns a blank cookie jar.  Maybe someone could update the documentation to point out what the mandatory fields are.
0
 
geekdad1Author Commented:
The helper pointed me to documentation that I had already read and re-reading it wasn't much help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now