Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Passing a cookie with LWP::UserAgent

by tphyahoo (Vicar)
on May 13, 2005 at 12:32 UTC ( #456694=note: print w/replies, xml ) Need Help??

in reply to Passing a cookie with LWP::UserAgent

I wound up with this code a while ago when I was troubleshooting some issues with cookies, logging into a google adwords account. I did eventually get it to work. YMMV...
my $cookie_jar = HTTP::Cookies->new; $cookie_jar->clear; # shouldn't be necessary since object was newl +y created in above line but this is all experimentation... # A variable that holds our POST request to the site $ENV{HTTPS_VERSION} = 3; my $action = POST ' +on', [ 'hl' => $language, 'login.userid' => $adwordsEmailLogin, 'login.password' => $adwordsPassword, 'login' => 'Login' ]; my $ua = LWP::UserAgent->new; $ua->cookie_jar($cookie_jar); # We disguise ourselves as Internet Explorer $ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'); $ua->timeout(30); #$ENV{HTTPS_PROXY} = ''; #if need prox +y if ($proxy) { logMessage( "adwordsLogin: Logging into adwords using proxy $p +roxy.\n"); $ua->proxy('http',$proxy); # should this be https? No, seems +to work as is. # actually, not sure. check this later. } my $www = $ua->request( $action ); # In the event that the request to google fails my $headers = new HTTP::Headers; $headers = $www->headers(); my $redir = $headers->header('Location'); #target url my $action2 = GET ''.$redir; my $www2 = $ua->request( $action2 ); unless ($www2->is_success) { logMessage( "AdwordsLogin: The error code: " . $www2->code . " + was returned!"); } $cookie_jar->extract_cookies($www2); # Put the HTML into a seperate variable #my $content = $www2->content; # test #open F, "> adwordsLoginResult.html"; print F $content; close F; $adwordsLoginTries++; return \$cookie_jar;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://456694]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2021-05-12 07:19 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (125 votes). Check out past polls.