Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Perl Programming Language

using LWP::UserAgent Get method


hi, there,

I am learning to use Perl to grab some useful information from
internet. For example to get specified web page.

I tested several web sites and the code works well. But when I tried
on www.officedepot.com, the code does not work any more. the returned
error message is always "Error1: when get http://www.officedepot.com:
500 Can't connect to www.officedepot.com:80 (connect: Unknown error)"

I have no idea what is going wrong.  please help me on this

regards

paul

////

use LWP 5.64;
use LWP::UserAgent;
use HTTP::Headers;
use HTTP::Cookies;

$browser = LWP::UserAgent->new;
$browser->timeout(5);
$url ='http://www.officedepot.com';
@ns_headers = (
'Accept'=>'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel, application/
vnd.ms-powerpoint, application/msword, */*',
'Accept-Language'=>'en-us',
'Accept-Encoding'=>'gzip, deflate',
'User-Agent'=>'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
SV1; .NET CLR 2.0.50727)',
'Proxy-Connection'=>'Keep-Alive',
);

$browser->cookie_jar( HTTP::Cookies->new(
   'file' => 'odcookies.lwp',
    # where to read/write cookies
    'autosave' => 1,
        # save it to disk when done
  ));

        $response = $browser->get($url, @ns_headers);
        if ($response->is_success)
        {
        print "Connected with server\n";
        }
        else
        {
                print "Error1: when get $url:\t" . $response->status_line . "\n";
        }

Paul wrote:
> hi, there,

> I am learning to use Perl to grab some useful information from
> internet. For example to get specified web page.

> I tested several web sites and the code works well. But when I tried
> on www.officedepot.com, the code does not work any more. the returned
> error message is always "Error1: when get http://www.officedepot.com:
> 500 Can't connect to www.officedepot.com:80 (connect: Unknown error)"

> I have no idea what is going wrong.  please help me on this

This code work for me

#!/usr/bin/perl
use strict;
use LWP::UserAgent;
our $ua = LWP::UserAgent->new(timeout => 60);
my $myagent='Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.8.1.3)
Gecko/20070309 Firefox/2.0.0.3';
$ua->agent($myagent);
my ($status,$content)=readweb('http://www.officedepot.com');
print $c;

sub readweb
{
my $w=shift;
my $cnt=0;
my ($req,$res);
$req = new HTTP::Request GET => $w;
$req->headers->header(Accept => '*/*');
$req->headers->header(Connection => "Keep-Alive");
$req->headers->header(Accept_language => 'cs,en-us;q=0.7,en;q=0.3');
$req->headers->header(Cache_Control => 'no-cache');
$req->headers->header(Keep_Alive => '300');
$res = $ua->request($req);
if ($res->is_error*1 > 0)
 {
 return ($res->{_rc},$res->{_msg});
 }
my $content=$res->content;
if ($content!~/<\/html>/si)
 {
 return ($res->{_rc},$res->{_msg});
 }
return ($res->{_rc},$content);

}

--
Petr

Skype: callto://fidokomik

Na mail uvedeny v headeru zpravy nema cenu nic posilat, konci to v PR*
:-) Odpovidejte na petr na practisoft cz

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc