How Do I Use Proxy in Net:HTTP?

P

Public Interest

Can anyone tell me how the proxy is implemented in LWP:UserAgent? I have to
use a proxy in Net:HTTP, but proxy is not supported in it.
 
G

Guest

Public Interest said:
Can anyone tell me how the proxy is implemented in LWP:UserAgent? I have to
use a proxy in Net:HTTP, but proxy is not supported in it.

Net::HTTP support proxies just fine. RFC 2616 explains how to do it.
You basically just provide full absolute URLs with the requests you
send.
 
B

Bart Lateur

Public said:
Please give me a line or two showing how to use proxy..

In UserAgent it is:
$ua->proxy(['http', 'ftp'], 'http://proxy.sn.no:8001/');

Can do use the same line in Net:Http? I don' know how use use 2616.

No. As Gisle Aas wrote, you have to do a low level connection yourself.
Thank goodness, the difference between a direct connection and a
connection via proxy is minimal. The gist is:

For a normal connection, you
- connect to a host/port, and
- request for a URL that looks like an absolute Unix path. (= relative
to the domain root)

For a proxy connection, you
- connect to the proxy, and
- request for an absolute URL, including protocol ("http"), domain and
optionally, port.

Here's an example modified from the Synopsis of Net::Http, which
requests a page on a port different from the default for http, 80:

use Net::HTTP;
my $s = Net::HTTP->new(Host => "modperl.com", PeerPort => 9000)
or die $@;
$s->write_request(GET => "/perl_networking/errata.html");
my($code, $mess, %h) = $s->read_response_headers;

print "Response code: $code ($mess)\n";
use Data::Dumper;
print Data::Dumper->Dump([\%h], ['*headers']);
print "\n";

{
my $n = $s->read_entity_body(my $buf, 1024);
die "read failed: $!" unless defined $n;
print $buf;
redo if $n;
}

Here is it again, for another URL, and modified to connect via the proxy
of my ISP (<http://proxy.pandora.be:8080>):

use Net::HTTP;
my $s = Net::HTTP->new(Host => "httpd.apache.org",
PeerAddr => 'proxy.pandora.be', PeerPort => 8080) or die $@;
$s->write_request(GET =>
"http://httpd.apache.org/docs/misc/FAQ.html");
my($code, $mess, %h) = $s->read_response_headers;

print "Response code: $code ($mess)\n";
use Data::Dumper;
print Data::Dumper->Dump([\%h], ['*headers']);
print "\n";

{
my $n = $s->read_entity_body(my $buf, 1024);
die "read failed: $!" unless defined $n;
print $buf;
redo if $n;
}

IMO the code to retrieve data via a proxy is actually simpler than for a
normal connection, because (almost) everything but the URL for the GET
is constant.

n.b. I think the Host property is mainly important if you encounter name
based virtual hosts, otherwise you could get pages from the wrong
domain.
 
P

Public Interest

Dear Master Bart,

Thank YOU.
My own code was:
my $s = Net::HTTP->new(Host => '64.161.246.100:8080') || die $@;
$s->write_request(GET => 'http://quote.yahoo.com', 'User-Agent' =>
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)' );

So I did not know the syntax of using proxy, because it was not said in it.

My final question on this: Why do I have to put host here? Because I put
peeradd which is proxy server, peerport and the abs url. Your said the Host
is important for virtual domain, but you already has the full url to let
httpd to know? Can I do?
my $s = Net::HTTP->new(
PeerAddr => 'proxy.pandora.be', PeerPort => 8080) or die $@;
$s->write_request(GET =>
"http://httpd.apache.org/docs/misc/FAQ.html");
or

my $s = Net::HTTP->new(Host => "httpd.apache.org",
PeerAddr => 'proxy.pandora.be', PeerPort => 8080) or die $@;
$s->write_request(GET =>
"/docs/misc/FAQ.html");


..
 
P

Public Interest

One of things I don't undertand is you and I both read the same document on
Net::Http, there is no such info as what I need, but you can write a correct
code and I could not. Do you have a better documation file or a better cook
book? What you explained to me on how to use the proxy is no where found.
Don't know how you first guessed the syntax right.
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top