<?xml version="1.0" encoding="windows-1252"?>
<node id="75770" title="(code) LWP with auth proxy" created="2001-04-26 12:54:57" updated="2005-08-09 10:40:15">
<type id="1980">
snippet</type>
<author id="14909">
ybiC</author>
<data>
<field name="doctext">
</field>
<field name="snippetdesc">
Example of LWP syntax for use with authenticated proxy.

&lt;p&gt;
2002-03-23  [cpan://Getopt::Long] and [cpan://Pod::Usage], minor format cleanup&lt;br&gt;
Minor updates 2001-04-27
&lt;/p&gt;

&lt;!-- 04/26 at 17:48:33 Petruchio says Dang... nice code! --&gt;</field>
<field name="snippetcode">
&lt;CODE&gt;#!/usr/bin/perl -w

# dget.pl
# pod at tail

use strict;
use LWP::UserAgent;
use Getopt::Long;
use Pod::Usage;


my ($opt_help, $opt_man);
GetOptions(
  'help!' =&gt; \$opt_help,
  'man!'  =&gt; \$opt_man,
) or pod2usage(-verbose =&gt; 1) &amp;&amp; exit;
pod2usage(-verbose =&gt; 1) &amp;&amp; exit if (defined $opt_help);
pod2usage(-verbose =&gt; 2) &amp;&amp; exit if (defined $opt_man);


# Begin config parameters
my %parm = (
  url       =&gt; shift,
  outfile   =&gt; shift, 
  uatimeout =&gt; 120,          # seconds before giving up on fetch
  browser   =&gt; 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.9) Gecko/20020310 ',
);
my %proxy = (
  host =&gt; '',    # http://host.dom:port
  id   =&gt; '',    # ntdom\userid
  pass =&gt; '',    # 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-&gt;agent($parm{browser});
$ua-&gt;timeout($parm{uatimeout});
$ua-&gt;proxy(http =&gt; "$proxy{host}") if (defined $proxy{host}); 
$parm{url} = new URI::URL($parm{url});
my $req    = new HTTP::Request "GET" =&gt; ($parm{url});
$req-&gt;proxy_authorization_basic
  ($proxy{id}, $proxy{pass}) if (defined $proxy{id});


my $res = $ua-&gt;request($req);
if ($res -&gt; is_success) {
  my $rescont = $res-&gt;content;
  open (OUT, "&gt;$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-&gt;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;
}



=head1 NAME

 dget.pl

=head1 SYNOPSIS

 dget.pl http://host.dom/page
 dget.pl [OPTION or ARGUMENT]

=head1 DESCRIPTION

 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

=head1 ARGUMENTS

None.  All arguments are defined in the code at 'config parameters'

=head1 AUTHOR

 ybiC

=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)

=head1 CHANGELOG

 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

=cut


&lt;/CODE&gt;</field>
</data>
</node>
