Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Please guide me to the right issue behind this.#!/usr/bin/perl -w # dget.pl # pod at tail use strict; use LWP::UserAgent; use Getopt::Long; use Pod::Usage; use URI::URL; use LWP::Debug qw(+); my ($opt_help, $opt_man); GetOptions( 'help!' => \$opt_help, 'man!' => \$opt_man, ) or pod2usage(-verbose => 1) && exit; pod2usage(-verbose => 1) && exit if (defined $opt_help); pod2usage(-verbose => 2) && exit if (defined $opt_man); # Begin config parameters my %parm = ( url => shift, outfile => shift, uatimeout => 120, # seconds before giving up on fetch browser => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.9) Geck +o/20020310 ', ); my %proxy = ( host => 'http://proxy.mycomp.com:8080', # http://host.dom:port id => 'mynw\mylogin', # ntdom\userid pass => 'mypasswd', # empty quotes if no proxy auth ); # End config parameters unless(defined $parm{url}) { print "\n Ooot - you forgot to provide a URL !\n"; Ooot(); } unless(defined $parm{outfile}) { print "\n Ooot - your forgot to provide an outfile !\n"; Ooot(); } print "\n Fetching $parm{url}...\n"; my $ua = new LWP::UserAgent; $ua->agent($parm{browser}); $ua->timeout($parm{uatimeout}); $ua->proxy(http => "$proxy{host}") if (defined $proxy{host}); $parm{url} = new URI::URL($parm{url}); my $req = new HTTP::Request "GET" => ($parm{url}); $req->proxy_authorization_basic ($proxy{id}, $proxy{pass}) if (defined $proxy{id}); my $res = $ua->request($req); if ($res -> is_success) { my $rescont = $res->content; open (OUT, ">$parm{outfile}") or die "Error opening $parm{outfile} for write: $!"; print OUT $rescont; close OUT or die "Error closing $parm{outfile}: $!"; } else { my $resmsg = $res->message; print "Error fetching $parm{url}:\n $resmsg\n\n"; exit; } print " Done! Page saved at '$parm{outfile}'\n\n"; sub Ooot { print "\n dget.pl --help", "\n dget.pl --man", "\n", "\n LWP $LWP::VERSION", "\n Perl $]", "\n OS $^O", "\n Program $0", "\n\n", ; exit; } Outputs the following : D:\testperl>perl dget.pl http://google.com out.txt Fetching http://google.com... LWP::UserAgent::new: () LWP::UserAgent::proxy: http http://proxy.mycomp.com:8080 LWP::UserAgent::request: () LWP::UserAgent::send_request: GET http://google.com/ LWP::UserAgent::_need_proxy: Proxied to http://proxy.mycomp.com:8080 LWP::Protocol::http::request: () LWP::Protocol::collect: read 623 bytes LWP::Protocol::collect: read 1754 bytes LWP::UserAgent::request: Simple response: Proxy Authentication Require +d Error fetching http://google.com/: Proxy Authentication Required ( The ISA Server requires authorizatio +n to fulfi ll the request. Access to the Web Proxy service is denied. )
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: LWP and proxy
by Fletch (Bishop) on May 08, 2007 at 17:25 UTC | |
by Anonymous Monk on May 09, 2007 at 13:28 UTC | |
|
Re: LWP and proxy
by cengineer (Pilgrim) on May 08, 2007 at 17:13 UTC | |
by Anonymous Monk on May 08, 2007 at 17:21 UTC |