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

I wish my question was so "SIMPLE" hehehehe that I could answer it. I ask for the power of the Seattle Monolith, and the perlmonks, for help on this question...

How do I set a proxy server when using the module LWP::Simple? I have looked through the documentation, and it says it uses the ones already define. Well how do you define them???

I am the first overweight, very tall munchkin... be very amazed.

Replies are listed 'Best First'.
Re: LWP::Simple setting a proxy
by t0mas (Priest) on Jan 17, 2001 at 21:13 UTC
    I usually go:
    $ENV{HTTP_proxy}="http://proxy.company.com:80";
    somewhere before I start getting stuff with it.

    But you can also set the environment variable HTTP_proxy in your shell to http://proxy.company.com:80

    /brother t0mas

      Thanks, but no success... I still get nope. So, maybe I am doing somethign wrong.

      use LWP::Simple; $ENV{HTTP_proxy}="http://10.2.7.11:8080"; #The username that you are looking up print "Please input username: "; $username = <STDIN>; chomp($username); #Gets URL $url = 'http://www.live365.com/cgi-bin/directory.cgi?genre=search&sear +chdesc=' . $username . '&searchfields=H'; print "$url\n"; unless (defined ($page = get($url))) { print "Cannot retrieve page... Sorry =(\n\n"; exit; } #Get Description print "Description: "; if($page =~ m|<A class="desc".*?>(.*?)</A>|) { print "$1\n"; } else{ print "No description\n"; } #Check Profile print "Profile: "; if($page =~ m|<A class="handle".*?>(.*?)</A>|) { print "$1\n"; } else{ print "No profile\n"; } #Connection Speed print "Speed: "; if($page =~ m|<TD ID="connection".*?>(.*?)</TD>|) { print "$1\n"; } else{ print "No connection\n"; } #Number of listeners print "Listeners: "; ($listen,$outof) = (0,0); if($page =~ m|DrawListenerStars("/scp/../images/", (\d+), (\d+))|) {($ +listen,$outof) = ($1,$2);} print "$listen / $outof\n";

      This is supposed to check live365.com and return the information of my radio. I know it might see like "Why are you doing this?", but when their web server is so damn slow. It makes it worth not loading their enitre page.

      I am the first overweight, very tall munchkin... be very amazed.
        I bet that LWP::Simple's internal UserAgent gets created at use time, so your env variable definition is not seen.

        Just guessing (untested)... either put your env-var setting inside a BEGIN block before the use:

        BEGIN { $ENV{HTTP_proxy}="http://10.2.7.11:8080" } use LWP::Simple;
        or you can always go "behind the scenes" with LWP::Simple by accessing $LWP::Simple::ua:
        use LWP::Simple qw($ua); $ua->proxy([qw(http ftp)], 'http://10.2.7.11:8080');

        -- Randal L. Schwartz, Perl hacker

        I would like to use HTTP::Request::Common and LWP::UserAgent for that kind of stunt and use a POST to submit the data to the server.

        Try out the following:
        #!/usr/bin/perl -w # Uses use strict; use HTTP::Request::Common qw(POST); use LWP::UserAgent; # URL my $url = 'http://www.live365.com/cgi-bin/directory.cgi'; # User Agent Object my $ua = LWP::UserAgent->new; $ua->proxy(['http', 'ftp'], 'http://10.2.7.11:8080'); #The username that you are looking up print "Please input username: "; my $username = <STDIN>; chomp($username); # Request my $req = POST $url, [ genre=>'search', searchdesc=> $username, searchfields=>'H']; my $page=$ua->request($req)->as_string; #The rest of your code goes here


        /brother t0mas
        Just a guess (since you gave no information on how or what is failing):

        Does your proxy, by any chance, require authentication?

        Have fun ...

        Andreas

Re: LWP::Simple setting a proxy
by ichimunki (Priest) on Jan 17, 2001 at 22:07 UTC
    I'd switch to HTTP::Request and UserAgent, which have much more robust and explicit methods for this sort of transaction.
Re: LWP::Simple setting a proxy
by wardk (Deacon) on Jan 17, 2001 at 22:09 UTC

    check out the documentation for LWP::UserAgent, you should find what you need there...