Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

(code) LWP with auth proxy

by ybiC (Prior)
on Apr 26, 2001 at 16:54 UTC ( #75770=snippet: print w/replies, xml ) Need Help??
Description: Example of LWP syntax for use with authenticated proxy.

2002-03-23 Getopt::Long and Pod::Usage, minor format cleanup
Minor updates 2001-04-27

#!/usr/bin/perl -w

# pod at tail

use strict;
use LWP::UserAgent;
use Getopt::Long;
use Pod::Usage;

my ($opt_help, $opt_man);
  '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://host.dom:port
  id   => '',    # ntdom\userid
  pass => '',    # empty quotes if no proxy auth
# End config parameters

unless(defined $parm{url}) {
  print "\n  Ooot - you forgot to provide a URL !\n";
unless(defined $parm{outfile}) {
  print "\n  Ooot - your forgot to provide an outfile !\n";
print "\n  Fetching $parm{url}...\n";

my $ua = new LWP::UserAgent;
$ua->proxy(http => "$proxy{host}") if (defined $proxy{host}); 
$parm{url} = new URI::URL($parm{url});
my $req    = new HTTP::Request "GET" => ($parm{url});
  ($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";

print "  Done!  Page saved at '$parm{outfile}'\n\n";

sub Ooot {
    "\n --help",
    "\n --man",
    "\n  LWP      $LWP::VERSION",
    "\n  Perl     $]",
    "\n  OS       $^O",
    "\n  Program  $0",

=head1 NAME

=head1 SYNOPSIS http://host.dom/page [OPTION or ARGUMENT]


 Command-line web-page fetcher
 Testbed for figuring out proxy authentication code

=head1 OPTIONS

 -h  --help  display Usage, Arguments, and Options
 -m  --man   display complete man page


None.  All arguments are defined in the code at 'config parameters'

=head1 AUTHOR


=head1 TESTED

 LWP 5.51+5.48   Perl 5.00503     Debian 2.2r3
 LWP 5.5392      Perl 5.00601     Cygwin on Win2kPro
 LWP 5.48        SiePerl 5.00503  Win2kPro (zsh|cmd.exe)
 LWP 5.48        ActivePerl 5.60  Win2kPro (zsh|cmd.exe)


 2002-03-23   22:45  CST
   Getopt::Long+Pod::Usage to eliminate sub Usage{...}
   Reformat code closer to PerlStyle recommendations

 2001-04-27   10:30
   Take outfile name from keyboard input instead of hard-coded
   More tests

 2001-04-26   11:00 
   More tests
   Post to PerlMonks Snippets
   Minor format tweaks

 2001-04-25   22:15
   Initial working code

=head1 TODO

 Getopt::Long for proxy, timeout, browser
 Save to outfile of same name as URL file
 Prompt for user+pass instead of hard-coded
 Update TESTED section of pod

Replies are listed 'Best First'.
Re: (code) LWP with auth proxy
by Anonymous Monk on May 04, 2007 at 08:43 UTC
    does not work....output..after loading URL::URI.. Error fetching Proxy Authentication Required ( The ISA Server requires authorization to fulfi ll the request. Access to the Web Proxy service is denied. )
      I've the same problem :/ Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )
Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://75770]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2022-08-12 14:10 GMT
Find Nodes?
    Voting Booth?

    No recent polls found