LWP::UserAgent HTTPs Debug

前端之家收集整理的这篇文章主要介绍了LWP::UserAgent HTTPs Debug前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1) The latest(v5.14) perl SSL lib implementation is quite different from the old one(v5.8.8). The following code can work well with the old version perl,but it can't live with the new perl on win7 64bit.

#!/usr/bin/perl
use warnings;
use strict;

use LWP::UserAgent;
#use LWP::Debug (+conn);

my @requestHeaders = (
 'Content-type' => 'text/json',
 'username' => 'xxx',
 'password' => 'yyy'
);

my $uri = 'https://yourhostname.com/content';
my $ua = LWP::UserAgent->new;
my $response = $ua->get($uri,@requestHeaders);
if($response->is_success)
{
  print $response->decoded_content;
}
else
{
  print $response->status_line;
}

2) In new perl,we need specify the certificate file which is exchanged with the SSL server (how to generate the CA file ? Will answer this question in future.). So the new code will look as below.

#!/usr/bin/perl
use warnings;
use strict;

use LWP::UserAgent;

my $uri = 'https://yourhostname.com/content';
my $ua = LWP::UserAgent->new;
$ua->ssl_opts(SSL_ca_file => './your_ca_file.crt(pem)'
my $response = $ua->get($uri);
if($response->is_success)
{
  print $response->decoded_content;
}
else
{
  print $response->status_line;
}

3) Tips,in the old perl,we can use LWP::Debug facility to trace the traffic. It is deprecated now. And in the new perl,we can install callback handler for tracing. And if tracing raw network traffic is needed,we can use Wireshark or tcpdump etc tools.
$ua->default_header('Accept-Encoding' => scalar HTTP::Message::decodable());
$ua->add_handler("request_send",  sub { shift->dump; return });
$ua->add_handler("response_done",sub { shift->dump; return });

4) More tips,sometime use 'curl','telnet' as the assistant tools to debug http(s) issue will be helpful.

猜你在找的Perl相关文章