Reputation: 85368
How can I print the values of the cookie/cookie_jar being set?
Trying:
##my $cookie_jar=HTTP::Cookies->new(file => "cookie.jar",autosave=>1,ignore_discard=>1);
my $cookie_jar=HTTP::Cookies->new(); ## Would like it to be in memory
my $agent = WWW::Mechanize->new(cookie_jar => $cookie_jar);
##my $agent = WWW::Mechanize->new();
##my $agent = WWW::Mechanize->new(autocheck => 1);
##$agent->cookie_jar( {} );
# we need cookies
##$agent->cookie_jar(HTTP::Cookies->new);
print "Set Cookie Jar?\n";
print $agent->cookie_jar->as_string();
print "\n";
$agent->get($url); // url is a https site
Not too much luck with any of these, what am I doing wrong?
Upvotes: 4
Views: 8756
Reputation: 13820
If you're looking for a value of a specific cookie, you will need to scan the whole cookie jar using HTTP::Cookie's $cookie_jar->scan( \&callback ) method. For example, to get JSESSIONID cookie from www.linkedin.com, you can use the following code:
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Cookies::Netscape;
my $cookies = HTTP::Cookies::Netscape->new(
hide_cookie2 => 1,
file => "$ENV{HOME}/.cookies.txt",
autosave => 1
);
my $browser = LWP::UserAgent->new(
env_proxy => 1,
autocheck => 1,
cookie_jar => $cookies,
agent => "get-jsessionid.pl/1.0"
);
$browser->env_proxy();
my $response = $browser->get( 'http://www.linkedin.com' );
if ($response->is_success)
{
$cookies->scan(sub
{
if ($_[1] eq 'JSESSIONID')
{
print "$_[1] @ $_[4] = $_[2]\n";
};
}
);
}
else
{
die $response->status_line;
}
The output would be something like:
JSESSIONID @ www.linkedin.com = "ajax:11122233344455556667"
Upvotes: 2
Reputation: 132896
Well, you have to have some cookies in the cookie jar to see any cookies in the output. So far you have an empty cookie jar. Either ensure that you add some cookies or that the site you are accessing sets them:
use HTTP::Cookies;
use WWW::Mechanize;
my $cookie_jar = HTTP::Cookies->new;
my $agent = WWW::Mechanize->new( cookie_jar => $cookie_jar );
$cookie_jar->set_cookie(
qw(
3
cat
buster
/
.example.com
0
0
0
)
);
$agent->get( 'http://www.amazon.com' );
print "Set Cookie Jar?\n", $agent->cookie_jar->as_string, "\n";
This gave me the output:
Set Cookie Jar?
Set-Cookie3: session-id=000-0000000-0000000; path="/"; domain=.amazon.com; path_spec; discard; version=0
Set-Cookie3: session-id-time=1272524400l; path="/"; domain=.amazon.com; path_spec; discard; version=0 Set-Cookie3: cat=buster; path="/"; domain=.example.com; port=0; version=3
However, you don't need to invoke HTTP::Cookies
directly. LWP
will take care of that. You just give cookie_jar
a hash reference:
my $agent = WWW::Mechanize->new( cookie_jar => {} );
If you just want the cookies from a particular response, you can create a separate cookie jar to hold the ones you extract from the response:
use WWW::Mechanize;
my $agent = WWW::Mechanize->new( cookie_jar => {} );
my $response = $agent->get( 'http://www.amazon.com' );
my $cookie_jar = HTTP::Cookies->new;
$cookie_jar->extract_cookies( $response );
print $cookie_jar->as_string;
Upvotes: 7
Reputation: 4778
Your main problem seems to be that you are trying to print the cookies before you actually visit the site. Try moving your print statements after your call to get()
Upvotes: 1