Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How would one make multiple attempts using LWP::UserAgent? I have the following snipped of code:
$req = HTTP::Request->new(GET => $reqstr); $req->authorization_basic('username', 'password'); $res = $ua->request($req);$req = HTTP::Request->new(GET => $reqstr); $req->authorization_basic('username', 'password'); $res = $ua->request($req); my $title = $ua->request($req)->header('Title'); if (($res->is_success) && ($title ne 'Empty result')) { open(OUT,"> $outfile") || die "Could not open $outfile for writing +: $!\n"; print OUT $ua->request($req)->content; close OUT; exit; ## Do not output if there were empty results } elsif ($res->status_line eq '200 OK') { print STDOUT "There were empty results\n"; #$attempts++; ## Some other error } else { print STDOUT "Error: " . $res->status_line . "\n"; $attempts++; sleep 10; } my $title = $ua->request($req)->header('Title'); while ($attempts < 4) { if (($res->is_success) && ($title ne 'Empty result')) { ## Open file handle for output file open(OUT,"> $outfile") || die "Could not open $outfile for writing +: $!\n"; print OUT $res; close OUT; exit; } elsif ($res->status_line eq '200 OK') { print STDOUT "There were empty results\n"; } else { print STDOUT "Error: " . $res->status_line . "\n"; }
My problem lies where I would want the loop to repeat if it's the third case (when it's equivalent to $res->is_error).

Replies are listed 'Best First'.
Re: Looping LWP::UserAgent
by ikegami (Patriarch) on Jun 07, 2005 at 00:46 UTC
    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!!

Re: Looping LWP::UserAgent
by tchatzi (Acolyte) on Jun 07, 2005 at 09:26 UTC
    I guess this is what you want
    use strict; my $attempts; my $outfile = "my_file.foo"; my $req = HTTP::Request->new(GET => $reqstr); $req->authorization_basic('username', 'password'); my $res = $ua->request($req); my $title = $res->header('Title'); while ($attempts ne 4){ if (($res->is_success) and ($title ne "")) { open(OUT,"> $outfile") or die "Could not open $outfile for writing: $!\n"; print OUT $res->content; close OUT; exit 0; ## Do not output if there were empty results } elsif ($res->status_line eq '200 OK') { print "There were empty results\n"; exit 0; ## Some other error } else { print "Error: " . $res->status_line . "\n"; print "Will try again in 10 seconds\n"; sleep 10; $attempts++; } }


    ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI