加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

LWP::UserAgent HTTPs Debug

发布时间:2020-12-16 00:23:07 所属栏目:大数据 来源:网络整理
导读: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 s

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读