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

I am somewhat proffecient in LWP but am having difficulty getting the following proxy method to work.

I have based this solution off of Toma's LWP Quick reference guide. If anyone knows a better way to do the proxy please let me know in easy to follow code.

use LWP::Simple; use LWP::Simple qw( $ua get ); use LWP::UserAgent; use HTTP::Request::Common qw(POST); my $ua = new LWP::UserAgent; $proxy = "72.43.50.184:80"; $contentget = "http://search.cpan.org/~gaas/libwww-perl-5.837/lib/LWP/ +UserAgent.pm"; $ua->timeout(3); $ua->proxy(['http'], '$proxy'); my $req = new HTTP::Request GET => '$contentget'; my $res = $ua->request($req); if ($res->is_success) { $content= $res->content; } else { die "Could not get content"; }

Result is:

Could not get content at C:\CC\BUY\ptest.pl line 22.

There is also the

$ua->proxy( 'http', $subproxy );my $content = get( $contentget )

But that doesnt work either. Any help would be appreciated.

POST UPDATE 1

------------------------------------------------------------------

Thank you for responding afoken

but when i tried adding the modifcations i get the following error. I tried escaping the / problem but still cant get it to work.

use LWP::Simple; use LWP::Simple qw( $ua get ); use LWP::UserAgent; use HTTP::Request::Common qw(POST); use HTTP::Request my $ua = new LWP::UserAgent; $proxy='http://72.43.50.184:80/'; $contentget = "http://search.cpan.org/~gaas/libwww-perl-5.837/lib/LWP/ +UserAgent.pm"; $ua->timeout(3); $ua->proxy(['http'], $proxy); my $req = new HTTP::Request GET => '$contentget'; my $res = $ua->request($req); if ($res->is_success) { $content= $res->content; } else { die "Could not get content"; } # ptest.pl
C:\CC\BUY>ptest.pl Backslash found where operator expected at C:\CC\BUY\ptest.pl line 30, + near "CC\" Backslash found where operator expected at C:\CC\BUY\ptest.pl line 30, + near "BUY\" syntax error at C:\CC\BUY\ptest.pl line 30, near "Could not " Execution of C:\CC\BUY\ptest.pl aborted due to compilation errors.

Replies are listed 'Best First'.
Re: LWP proxy setup difficulty
by afoken (Chancellor) on Oct 18, 2010 at 15:24 UTC

    I think LWP needs the proxy in URL format, i.e. $proxy='http://72.43.50.184:80/';.

    Also, remove the non-interpolating quotes around $proxy in $ua->proxy(['http'], '$proxy'); or you tell LWP to search for a server with a host name of $proxy.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: LWP proxy setup difficulty
by Khen1950fx (Canon) on Oct 18, 2010 at 21:54 UTC
    I noticed a few problems in your script. You're doing way too much. I cleaned it up and fixed the problems.
    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Request::Common; my $proxy = 'http://72.43.50.184:80/'; my $contentget = 'http://search.cpan.org/~gaas/libwww-perl-5.837/lib/L +WP/UserAgent.pm'; my $ua = LWP::UserAgent->new; $ua->timeout(30); $ua->proxy(['http'], $proxy); my $res = $ua->get($contentget); if ($res->is_success) { print $res->decoded_content; } else { die $res->status_line; }
Re: LWP proxy setup difficulty
by Monkomatic (Sexton) on Oct 18, 2010 at 15:55 UTC

    Thank you again.

    Your comments worked as soon as i figured out i left the results in my code by accident. doh

    thanks again.

    I knew it was something simple.....