Perl LWP::UserAgent cannot connect to HTTPS

5.3k views Asked by At

I have a script which is used to get content from Google. It work very well, but now it doesn't. I found a post on stackexchange and I upgrade the library version, but it still doesn't work: I cannot Connect to any HTTPS site using LWP::UserAgent

I have connectivity from the linux machine (telnet googleapis.com 443 works very well).

#!/usr/bin/perl


use CGI 'param';
use CGI::Carp 'fatalsToBrowser';
use DBI;
    require LWP::UserAgent;
    use LWP::Protocol::https;
    use URI::Escape;
    $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
    $access_token='xxx';
    print "LWP::UserAgent: ".LWP::UserAgent->VERSION,"\n";
    print "LWP::Protocol::https: ".LWP::Protocol::https->VERSION,"\n";
    $url="https://www.googleapis.com/oauth2/v1/userinfo?access_token=$access_token";
        my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
$ua->agent('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36');
        $ua->timeout(10);
        $ua->env_proxy;
        my $response = $ua->get("$url");
        if ($response->is_success) {
        print "Am adus cu succes contul de la google";
            $text=$response->decoded_content;  # or whatever
        }
        else {
            print "Response error:".$response->status_line."\n";

        }


1;

The error: 500 Can't connect to www.googleapis.com:443

Any idea why this is suddenly happen?

2

There are 2 answers

0
Adrian On

In some cases, you need to force SSLv3

my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0, SSL_version => 'SSLv3' });
0
Ilia Ross On

You need to install LWP::Protocol::https using CPAN or by package name perl-LWP-Protocol-https.

What worked for me, is installing it by package name on CentOS by running as root
yum install perl-LWP-Protocol-https

After that https links are opened as they should, with out an empty response.