Perl LWP Tunnel Proxy

Perl LWP::UserAgent integrating 16Yun Crawler Proxy.

16Yun Engineering TeamApr 25, 20261 min read

Perl LWP

use strict;
use warnings;
use LWP::UserAgent;
use JSON;
 
my $host = $ENV{PROXY_HOST} || 't.16yun.cn';
my $port = $ENV{PROXY_PORT} || '31111';
my $user = $ENV{PROXY_USERNAME} || 'user';
my $pass = $ENV{PROXY_PASSWORD} || 'password';
my $tunnel = $ENV{PROXY_TUNNEL} || 'perl-demo';
 
my $ua = LWP::UserAgent->new(
    proxy => {
        http  => "http://$host:$port",
        https => "http://$host:$port",
    },
    timeout => 15,
    ssl_opts => { verify_hostname => 0 },
);
 
# Proxy Authentication
$ua->credentials("$host:$port", "", $user, $pass);
 
my $req = HTTP::Request->new(GET => "https://httpbin.org/ip");
$req->header("Proxy-Tunnel" => $tunnel);
 
my $resp = $ua->request($req);
if ($resp->is_success) {
    print $resp->decoded_content, "\n";
} else {
    print "Error: ", $resp->status_line, "\n";
}

Four Scenarios

ScenarioLWP Implementation
A: Force SwitchCreate new LWP::UserAgent each time
B: Keep IPReuse $ua instance
C-HTTP:Proxy-Tunnel$req->header("Proxy-Tunnel", ...)
C-HTTPS: Proxy-TunnelDoes not support CONNECT custom headers

Perl's LWP::UserAgent configures proxies via the proxy parameter and handles authentication via the credentials method. HTTPS Proxy-Tunnel cannot inject custom headers during CONNECT.

Need an enterprise proxy plan?

We can tailor architecture to your target domains, concurrency, and reliability goals.