Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Proxy using Perl

by mosiurkhan (Initiate)
on Aug 29, 2007 at 11:31 UTC ( [id://635771]=perlquestion: print w/replies, xml ) Need Help??

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

how to proxy using LWP::userAgent? I have written a code shown below. But I can not set proxy using an IP from a bundle of proxy IPs. If anybody knows, please help me.
#!/usr/bin/perl

use LWP::UserAgent;
use HTTP::Request;

print "Content-type: text/html\n\n";
print "<html><body>";

$userIP = "123.123.123.123";
$httpReferer = "hr";
$userAgent = "ua";

$url = "http://my.website.com/abc/";
$request = HTTP::Request->new(GET=>$url);
$request->referer($httpReferer);

$agent = LWP::UserAgent->new();
$agent->agent($userAgent);

$agent->timeout(10);
$proxy = "http://" . $userIP;
$agent->proxy('http',$proxy);

$msResponse = $agent->request($request);
$msResponse = $agent->get($url);

if($msResponse->is_success)
{
$msContent = $msResponse->content;
print $msContent;
}
else
{
print $msResponse->status_line;
}
- - - - - - - -- - -- - - - - - - - - - - - - - - - -

Replies are listed 'Best First'.
Re: Proxy using Perl
by ides (Deacon) on Aug 29, 2007 at 14:37 UTC

    That looks like it should work. Whats is the error message you receive?

    Frank Wiles <frank@revsys.com>
    www.revsys.com

      you're actually requesting the URL twice in your code for every request to the page, replace the $request line into this: my $request = new HTTP::Request; The rest seems to work. Oh yeah, notice the 'my'. try using strict mode when programming cgi's glenn
Re: Proxy using Perl
by andyford (Curate) on Aug 29, 2007 at 17:02 UTC

    I ran your code substituting my proxy and a real website and it worked. Perhaps your proxy is running on an alternative port (mine is on 8080). In that case, you want to add a colon and your port number after the IP address of the proxy (123.123.123.123:8080, eg.)

    Another possibility is that your proxy requires authentication to use. From the docs:

    $agent->proxy(['http', 'ftp'] => 'http://username:password@proxy.myorg +.com');

    non-Perl: Andy Ford

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://635771]
Approved by randyk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found