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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值