How Do I Use Proxy in Net:HTTP?

Discussion in 'Perl Misc' started by Public Interest, Oct 15, 2003.

  1. 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.
     
    Public Interest, Oct 15, 2003
    #1
    1. Advertising

  2. Public Interest

    Helgi Briem Guest

    On Wed, 15 Oct 2003 18:26:03 GMT, "Public Interest" <>
    wrote:

    >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.


    $ua->proxy(['http'] => "http://$proxy:$port");
     
    Helgi Briem, Oct 15, 2003
    #2
    1. Advertising

  3. Public Interest

    Guest Guest

    "Public Interest" <> writes:

    > 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.

    --
    Gisle Aas
     
    Guest, Oct 15, 2003
    #3
  4. 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.

    <> ????
    news:-did-not-set--mail-host-address--so-shoot-me...
    > "Public Interest" <> writes:
    >
    > > 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.
    >
    > --
    > Gisle Aas
     
    Public Interest, Oct 19, 2003
    #4
  5. Public Interest

    Bart Lateur Guest

    Public Interest wrote:

    >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.

    --
    Bart.
     
    Bart Lateur, Oct 19, 2003
    #5
  6. 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");



    ..
     
    Public Interest, Oct 20, 2003
    #6
  7. 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.
     
    Public Interest, Oct 20, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Great Deals
    Replies:
    1
    Views:
    121
  2. nntp
    Replies:
    12
    Views:
    379
    Joe Smith
    Oct 23, 2004
  3. Rhugga

    How to get CPAN to use Http Proxy?

    Rhugga, Oct 24, 2005, in forum: Perl Misc
    Replies:
    10
    Views:
    638
  4. jck11
    Replies:
    3
    Views:
    160
  5. Greg
    Replies:
    4
    Views:
    320
    Steven Simpson
    Aug 16, 2012
Loading...

Share This Page