Checking valid proxies through LWP::UserAgent

N

Naveen

Hi,
I have a simple subroutine which takes in a specified URL and a proxy
address from a list, and then verify whether that proxy is working for
the given URL. If it is working, then the content of the page is
displayed as output, else, error is reported.

#######
sub preview
{
$proxy=param('proxy');
print "$proxy-$url-<BR>";

require LWP::UserAgent;
$ua = new LWP::UserAgent;
#$ua->proxy( 'http', '$proxy');
$ua->proxy( ['http'] => "$proxy");

my $req = new HTTP::Request ('GET', "$url");
#print $ua->request($req)->as_string;

$res = $ua->request($req);
if ($res->is_success)
{
print $res->content;
}
else
{
print "Error: " . $res->status_line . "\n";
}
}
#######
But when i execute the above, i get the error "Error: 501 Protocol
scheme '' is not supported" (it does not seem to get the 'http' in the
code, shows the quotes minus the http). If i replace the 'http' with
anything else, then the output is displayed, but obviously, the page is
not being fetched through the proxy then....

Any clues....

Thanks
Naveen
 
N

Naveen

Thanks Chris,
Its working like a charm!

Naveen


Christian said:
Naveen said:
Hi,
I have a simple subroutine which takes in a specified URL and a proxy
address from a list, and then verify whether that proxy is working for
the given URL. If it is working, then the content of the page is
displayed as output, else, error is reported.

#######
sub preview
{
$proxy=param('proxy');

What does param('proxy') return? I suspect it contains just a
hostname. And has $proxy been declared in the main program body,
or did you leave out the
use strict;
use warnings;
on top of the script?

If $proxy is only used in this sub, it should read
my $proxy = param('proxy');
print "$proxy-$url-<BR>";

require LWP::UserAgent;
$ua = new LWP::UserAgent;

Same as above,
my $ua = ....
#$ua->proxy( 'http', '$proxy');

Well, the single quotes here wouldn't work out anyways...
$ua->proxy( ['http'] => "$proxy");

...and no need to quote $proxy here either.

If you read "perldoc LWP::UserAgent" again, you will see that
proxy notation has to be "Protocol://host:port/". If you pass only
a hostname, LWP::UserAgent parses it as an empty protocol name,
therefore the message you get.

To give a working example:
--------------------------------------------------------------
#!/usr/bin/perl

use strict;
use warnings;
use LWP::UserAgent;

my $proxy = 'http://proxy.mydomain.com:3128/';
my $url = 'http://www.google.com';

my $ua = new LWP::UserAgent;

$ua->proxy( ['http'] => $proxy );
my $res = $ua->get( $url );

print $res->content if( $res->is_success );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top