LWP::UserAgent HTTPs Debug

本文探讨了Perl从旧版本v5.8.8升级到最新版本v5.14后SSL库的变化,并介绍了如何调整代码以适应新版本的要求。包括指定证书文件的方法、调试技巧的更新以及使用外部工具进行辅助调试。

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.


[root@localhost yum.repos.d]# cpanm -f Net::HTTP GSSAPI Params::Validate Test::Mock::LWP LWP::Authen::Negotiate --> Working on Net::HTTP Fetching https://www.cpan.org/authors/id/O/OA/OALDERS/Net-HTTP-6.23.tar.gz ... OK Configuring Net-HTTP-6.23 ... OK Building and testing Net-HTTP-6.23 ... OK Successfully installed Net-HTTP-6.23 --> Working on GSSAPI Fetching https://www.cpan.org/authors/id/A/AG/AGROLMS/GSSAPI-0.28.tar.gz ... OK Configuring GSSAPI-0.28 ... N/A ! Configure failed for GSSAPI-0.28. See /root/.cpanm/work/1752032584.51560/build.log for details. --> Working on Params::Validate Fetching https://www.cpan.org/authors/id/D/DR/DROLSKY/Params-Validate-1.31.tar.gz ... OK Configuring Params-Validate-1.31 ... OK Building and testing Params-Validate-1.31 ... OK Successfully reinstalled Params-Validate-1.31 --> Working on Test::Mock::LWP Fetching https://www.cpan.org/authors/id/L/LU/LUKEC/Test-Mock-LWP-0.08.tar.gz ... OK Configuring Test-Mock-LWP-0.08 ... OK Building and testing Test-Mock-LWP-0.08 ... OK Successfully reinstalled Test-Mock-LWP-0.08 --> Working on LWP::Authen::Negotiate Fetching https://www.cpan.org/authors/id/A/AG/AGROLMS/LWP-Authen-Negotiate-0.08.tar.gz ... OK Configuring LWP-Authen-Negotiate-0.08 ... OK ==> Found dependencies: GSSAPI, LWP::Debug --> Working on LWP::Debug Fetching https://www.cpan.org/authors/id/O/OA/OALDERS/libwww-perl-6.79.tar.gz ... OK Configuring libwww-perl-6.79 ... OK Building and testing libwww-perl-6.79 ... OK Successfully installed libwww-perl-6.79 ! Installing the dependencies failed: Module 'GSSAPI' is not installed ! Bailing out the installation for LWP-Authen-Negotiate-0.08. 4 distributions installed [root@localhost yum.repos.d]#
最新发布
07-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值