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!!


In reply to Re: Looping LWP::UserAgent by ikegami
in thread Looping LWP::UserAgent by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.