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

Hi All, When I run this perl program from home it seems to be working. But at office I get the following error message "Error: 500 Can't connect to www.yahoo.com:80 (connect: Unknown error)" . I guess it may be due to the proxy. Here is the code snippet. Can anyone let me know how to set the proxy userid and password in perl code.
#!/usr/bin/perl use LWP::UserAgent; $ua = LWP::UserAgent->new; $ua->agent("Mozilla/5.0"); # pretend we are very capable browser $req = HTTP::Request->new(GET => 'http://www.yahoo.com'); $req->header('Accept' => 'text/html'); # send request $res = $ua->request($req); # check the outcome if ($res->is_success) { print $res->content; } else { print "Error: " . $res->status_line . "\n"; }

Replies are listed 'Best First'.
Re: Get URL perl program
by Fletch (Bishop) on Dec 15, 2004 at 15:26 UTC

    perldoc lwpcook, look for the section titled PROXIES.

Re: Get URL perl program
by gellyfish (Monsignor) on Dec 15, 2004 at 15:29 UTC

    You need to set the environment variable HTTP_PROXY appropriately before you start the program and then add  $ua->env_proxy; before you make the request and all should be well. There is more about this in the LWP::UserAgent manual.

    /J\

Re: Get URL perl program
by ysth (Canon) on Dec 15, 2004 at 16:00 UTC
    You can also look at what lwp-request does. (See perldoc lwp-request and search the source for $options{'p'} and $options{'P'}.)