954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to reconnect to the online URL and download the data?

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 "$result";


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

Vandithar
Light Poster
37 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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.

wickedxter
Newbie Poster
20 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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

Vandithar
Light Poster
37 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 
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.

wickedxter
Newbie Poster
20 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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";
}
KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You