Hi,

I have a URL like this:

$url="http://www.somesite.com";

I have to connect to this URL and download the data. Sometimes there will be problem in that particular site. So I have to reconnect and then again download the data.

Here is the code to connect online.

#!/usr/bin/perl 

use CGI;
use LWP;
use DBI;
use CGI qw(:standard);
use LWP::UserAgent;

$fetch="http://www.somesite.com";

$ua=new LWP::UserAgent;
$res=$ua->get($fetch);
$result=$res->content;
print "<br>$result<br>";

How can i write a program such that if a data can't be downloaded at first time suppose if any problem persists how can i reconnect to that perticular site and download the data ?

If it can try connecting 5 times and downloading the data and if 5th time also it fails then the program should stop try connecting automatically.

How can i do this?

Any ideas??

Regards
Vanditha

add this

if ($res->is_success) {
      print $res->content;
  }
  else {
      print $res->status_line, "\n";
  }

this was taken from the cpan site on this module.

add this

if ($res->is_success) {
      print $res->content;
  }
  else {
      print $res->status_line, "\n";
  }

this was taken from the cpan site on this module.

Hi,

Thanks for the reply.

I have one doubt how can i check for 5 times using the above code i.e i will try reconnecting 4 times if i try to reconnect 5th time it should display saying that "Can't try now!!! try little later".

How can i do this???

Any ideas???

Regards
Archana

use LWP::UserAgent;

my $ua = LWP::UserAgent->new;

my $counters = {start => 0, end =>5};

REQUESTS:
my $req = $ua->get( 'http://some.web.site');

if ($req->is_success) {
      print $req->content;
  }
  else {
        $counters->{start}++ if $counters->{start} <= $counters->{end}; 
       print "Failed to retrive site, try again later." if $counters->{start} eq $counters->{end};

GOTO REQUESTS if $counters->{start} <= $counters->{end};
}

this is off the top of my head, you may need to tinker with it to get it work.

or using a loop:

use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $success = 1;
for (1..5) {
   my $req = $ua->get( 'http://some.web.site');
   if ($req->is_success) {
      print $req->content;
      $sucess = 0;   
      last;
  }
}
unless ($sucess) {
    print "Failed all 5 tries to get content\n";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.