deckoff
deckoff

Reputation: 361

Save cookies after post method, perl, mechanize, redirect

I want to login via perl script to http://www.tennisinsight.com/match_previews.htm and download the page. I am stuck at login into the site via the script

1 The site uses cookies to store login data.

2 The login form is triggered by javascript, but that is not important, because a simple web page on local that containts only:

  <form action="http://www.tennisinsight.com/myTI.php" method="POST">
      <input name="username" type="text" size="25" />
      <input name="password" type="password"  size="25" />
      <input name="mySubmit" type="submit" value="Submit!" />
 </form>

given the right username and pass will send the needed data, and the site will redirect to main page, user logged, and cookies are created. In short, a simple post with correct data does it all on the client side.

3.I have successfully tried and fetched the page I need with curl, once the correct cookies were provided.

I think that posting to myTI.php, storing the returned cookies and then opening the correct page while reading the cookies will do the trick, but I am failing at the save cookies part....

Here is the script I try to get cookies, it prints them in stdout at the moment

use warnings;
use HTML::Tree;
use LWP::Simple;
use WWW::Mechanize;
use HTTP::Request::Common;
use Data::Dumper;

my $username = "user";
my $password = "pass";
my $site_url = 'http://www.tennisinsight.com/myTI.php'; 
my $mech = WWW::Mechanize->new( autocheck => 1 );


# print $mech->content;

my $response = $mech->post($site_url , 
    [
       'username' => $username,
       'password' => $password,
    ]) ;
my $cookie_jar = HTTP::Cookies->new;
$cookie_jar->extract_cookies( $response );

print $cookie_jar;

EDIT: I have found the examples how to store cookies, the problem is that I get an empty file ( or empty stdout... It seems the called php will redirect before the cookies are stored and login will fail

I am sorry, but I am new to perl in general, seems I am missing something

Upvotes: 1

Views: 1268

Answers (2)

Trenton
Trenton

Reputation: 11996

I had the same problem, what looked like Mechanize not passing cookies between requests. To debug, I had Mechanize write the cookies to disk

my $mech = WWW::Mechanize->new(file => "/path/to/cookies");

When I did that, I got a file with this as the contents (i.e. the "empty" cookies file):

#LWP-Cookies-1.0

As gangabass suggested, I changed my user agent

$mech->agent_alias('Linux Mozilla');

and then cookies started appearing in the file and they were passed between subsequent requests. Problem solved.

It was the agent_alias call that fixed it, not writing the cookies to disk.

Upvotes: 1

wsdookadr
wsdookadr

Reputation: 2672

According to the HTTP::Cookies module documentation you can provide the following arguments to the constructor in order for the cookies to be stored on disk.

  • file => "/path/to/cookies"
  • autosave => 1

The method load is also present in order to load the cookies back from disk.

Upvotes: 0

Related Questions