in reply to Looping LWP::UserAgent
use strict; use warnings; my $tries = 4; my $sleep = 10; my $req = HTTP::Request->new(GET => $reqstr); $req->authorization_basic('username', 'password'); my $res; my $attempts; my $success; for (;;) { ++$attempts; $res = $ua->request($req); if ($res->is_success) { my $title = $res->Header('title'); if ($title ne 'Empty result') { $success = 1; last; } #else { print("Error: Empty results\n"); } } #else { print "HTTP Error: " . $res->status_line . "\n"; } last if $attempts >= $tries; # Sleep 10 seconds and try again. #print("Trying again in $sleep seconds...\n"); sleep($sleep); } die("Unable to download data after $tries tries.\n"); unless $success; open(my $out_fh, '>', $outfile) or die("Could not open $outfile for writing: $!\n"); print $out_fh $req->content;
In your original code, you called $ua->request($req) 4 times before the while. That means that before you even started looping, you were requesting the page 4 times!!
|
|---|