How to release or kill Mechanize session

Discussion in 'Perl Misc' started by dysgraphia, Mar 13, 2007.

  1. dysgraphia

    dysgraphia Guest

    I am using scripts like the following to retrieve web data.
    After login I use another instance of $mech->get($url) to
    get data from links on the retrieved page.
    Sometimes I get a login failure which may be due to the page
    timing out. Rather than have this happen I decided to deliberately
    end each session after a few downloads then do the login again.
    If I enclose my script with the statements perl.begin; and perl.end;
    this seems to have worked.

    My question: Is this the correct way to kill or release a Mechanize session?
    Any comments or suggestions welcome...cheers, Peter

    #!/user/bin/perl -w
    use strict;
    use warnings;

    use WWW::Mechanize;
    use DBI;
    use HTTP::Cookies;
    use Win32::ODBC;

    # Do the login...
    my ($url,$username,$password);
    $url = 'http://www.xxxxxx.com/login.asp';
    $username = "xxxx";
    $password = "xxxx";
    my $mech = WWW::Mechanize->new(
    agent => 'Mozilla/4.0',
    cookie_jar => {} );
    $mech->get($url);
    unless ($mech->success)
    {
    die "Can't get the login page $url: ",
    $mech->response->status_line;
    }
    $mech->field(p_login => $username);
    $mech->field(p_password => $password);
    $mech->click();

    my $url1 = 'http://www.xxxxxx.com/';

    $mech->get($url1);
    unless ($mech->success)
    {
    die "Can't get the login page url1 $url1: ",
    $mech->response->status_line;
    }

    print "Now retrieving: "$url1,"\n";

    # The retrieved page may have up to 10 links
    # create an array of the links
    my @links = $mech->links;
    # loop through the links
    foreach my $link (@links)
    {
    my $href = $link->url;
    # Retrieve the link text
    my $name = $link->text;
    print "href: " , $href, "\n" , "name: " , $name , "\n" ;
    sleep(3);
    }
    dysgraphia, Mar 13, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mohammad Samara

    Kill session / force sign out

    Mohammad Samara, Jan 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    743
    Mark Fitzpatrick
    Jan 10, 2004
  2. Jeff Dillon
    Replies:
    10
    Views:
    1,725
    Jeff Dillon
    Nov 5, 2004
  3. Andreas Klemt
    Replies:
    1
    Views:
    3,954
    Brock Allen
    Apr 15, 2005
  4. Fabrice
    Replies:
    2
    Views:
    8,463
    Marina
    Sep 6, 2005
  5. George Lake
    Replies:
    2
    Views:
    6,069
    Greg Young
    May 10, 2006
Loading...

Share This Page