ladyscifi has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a very simple script to get a web page and save it as a file. The only wrinkle is I can't seem to get past my company's proxy. Since the customer will be going through a proxy as well, I have to get past this little issue.

I have tried using $ENV{HTTP_PROXY} combined with useragent->env_proxy().

I get "502 Proxy Error ( The ISA Server denies the specified Uniform Resource Locator (URL). )" whether the credentials supplied are good or not.
Here is the code for this approach:

use strict; use warnings; use Getopt::Long; use LWP::UserAgent; use HTTP::Request::Common qw(GET); my $url; sub usage() { die "Usage: $0 --url=http://my.yahoo.com/ \n"; } GetOptions('url=s' => \$url); if (! defined $url ) { usage(); } my $ua = LWP::UserAgent->new(env_proxy => 1, keep_alive => 1, timeout +=> 30,); $ua->agent("Mozilla/6.0"); # Define user agent proxy #$ua->proxy(['http', 'ftp'] => 'http://nicolew:password@192.168.1.10') +; #$ua->proxy(['http', 'https'] => 'https://192.168.1.10:8080'); print "username: "; my $username = <STDIN>; chomp($username); print "password: "; my $password = <STDIN>; chomp($password); $ENV{HTTP_PROXY} = 'http://$username:$password@192.168.1.10:8080'; $ENV{HTTPS_PROXY} = 'https://$username:$password@192.168.1.10:8080'; $ua->env_proxy(); print "proxy is $ENV{HTTP_PROXY} \n"; print "url is $url \n"; # Request object my $req = HTTP::Request->new('GET', "$url"); #$req->proxy_authorization_basic($username,$password); # Make the request my $res = $ua->request($req); # Check the response if ($res->is_success) { print $res->content; } else { print $res->status_line . "\n"; } exit 0;

I tried HTTP::Request->proxy_authorization_basic combined with useragent - but no proxy setting and I get: "500 Can't connect to my.yahoo.com:80 (connect: Unknown error)"
Here is the code for this approach:

use strict; use warnings; use LWP::UserAgent; my $wsr = LWP::UserAgent -> new; $wsr -> timeout( 5 ); print "username: "; my $username = <STDIN>; chomp($username); print "password: "; my $password = <STDIN>; chomp($password); #$ENV{HTTP_PROXY} = 'http://192.168.1.10:8080'; #$wsr->credentials('http://192.168.1.10:8080/', 'IR', $username => $pa +ssword); #$wsr->env_proxy(); my $url = shift or die "URL expected\n"; my $request = HTTP::Request->new( HEAD => $url ); $request->proxy_authorization_basic($username,$password); my $response = $wsr->get($url); print $response->status_line, "\n"; print $response->is_success, "\n";
Help! and Thanks. :)
Nicole
Solutions Consultant
Integrated Research

Replies are listed 'Best First'.
Re: Problem with proxy and LWP
by Eliya (Vicar) on Jun 09, 2011 at 19:42 UTC
    $ENV{HTTP_PROXY} = 'http://$username:$password@192.168.1.10:8080'; $ENV{HTTPS_PROXY} = 'https://$username:$password@192.168.1.10:8080';

    Single quotes don't interpolate (the $username and $password), so you want double quotes here (but don't forget to escape the @).

    Not sure if that's the only problem, but it's at least something you probably expected to behave differently.

      Right you are. However that's a recent change made while very tired. Earlier attempts had it correctly as :
      $ENV{HTTP_PROXY} = "http://$username:$password\@192.168.1.10:8080";

      Unfortunately, I get the same result whether the quotes are correct or not. If it were simply a matter of bad credentials, wouldn't I get a different error? (not the 502 Proxy error)

Re: Problem with proxy and LWP
by natxo (Scribe) on Jun 10, 2011 at 14:26 UTC

      Unfortunately, I won't be able to change the customer's firewall. Here's my latest attempt:

      use strict; use warnings; use LWP::UserAgent; use LWP::Authen::Ntlm; use HTTP::Request::Common; print "creating agent\n"; my $ua = LWP::UserAgent->new(keep_alive=>1); $ua->timeout(10); print "setting credentials\n"; $ua->credentials('http://192.168.1.6:8080', '', 'IR\\nicolew', 'asdf') +; my $req = GET 'http://www.yahoo.com'; print "Getting site...\n"; my $res = $ua->request($req); print "returning info\n"; if ($res->is_success) { print $res->decoded_content; } else { print $res->status_line, "\n"; } exit 0;
      and the output is:

      creating agent
      setting credentials
      Getting site...
      returning info
      500 Can't connect to www.yahoo.com:80 (connect: Unknown error)
        I forgot to add that this same code works fine when I run it from my home network (no firewall, no proxy). I don't know which element is causing my issues. I suspect it's not the proxy just because the error is a 500 and not a 407.