Hi shmuel,

My experience is with GET, not POST, but I think that the cookies are
handled the same.

Here is a utility I often use in many robots:

# ----------------------- Preparing a "user-agent" -----------------------

use strict;
use WWW::Mechanize;
use HTTP::Cookies;
use LWP::ConnCache;

sub mech_user_agent {
  # In at least one application, we need to clear the cookie jar before
  # each new download. Therefore, we pass to the sub a reference to an
  # external scalar that holds the cookie object and will therefore be
  # able to clear it. Here it might be good idea to make the external
  # cookie_jar optional. Not all applications might need access to it. 

  my ($cookie_jar_ref) = @_;
  
  # Create a session cookies repository. This is the key for server
  # interaction. It is essential to obtain a session cookie, store it
  # in a "cookie jar" (variable) and keep it across requests. It locks
  # the requested dataset at the server which is then exported to TSV
  # (or CSV or Excel 2003).

  $$cookie_jar_ref = HTTP::Cookies->new or return undef;

  # Creating a session cache:
  my $conn_cache = LWP::ConnCache->new or return undef;

  # Create a Mechanize session with our session cache and cookie jar
  
  my $mech = WWW::Mechanize->new(
    conn_cache => $conn_cache,
    cookie_jar => $$cookie_jar_ref,
    onerror    => \&do_next,
  ) or return undef;
  # Add some headers to emulate a Firefox Browser.
  $mech->add_header('User-Agent','Mozilla/5.0(Windows;'
                   .'U; Windows NT 5.1;'
                   .'es-ES; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12');
  $mech->add_header('Accept','text/html,application/xhtml+xml,'
                   .'application/xml;q=0.9,*/*;q=0.8');
  $mech->add_header('Accept-Language','en-us;q=0.8,en;q=0.3');
  $mech->add_header('Accept-Encoding','gzip,deflate');
  $mech->add_header('Accept-Charset','utf-8;q=0.7,*;q=0.7');
  
  return $mech;
}

# ----------- bypassing the WWW::Mechanize default error trap -----------
# It simply returns an 'undef' so the script doesn't croak. I later
# check for success and if not loop again in another $attempt.

sub do_next() {
  return undef;
}

return 1;

# -----------------------------------------------------------------------

I hope it helps you

Meir

_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to