2

I am trying to get the contents of a website and print. The code worked how I wanted it to work with a regular HTTP website, but it will not work with HTTPS.

I have looked up fixes for this issue, but they are not working in my program. This is the code I currently have:

#! usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use LWP::UserAgent;
use 5.014;

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

my $ua = LWP::UserAgent->new();
$ua->ssl_opts( verify_hostnames => 0 );

getprint('https://<website>')or die 'Unable to get page';

And this is the error I am getting:

500 Can't connect to <IP address>:443 (certificate verify failed) <URL:https://<website>>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CircuitB0T
  • 465
  • 2
  • 6
  • 19

2 Answers2

4

Perhaps the following will be helpful:

use strict;
use warnings;
use LWP::UserAgent;
use open qw(:std :utf8);

my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } );
my $response = $ua->get('https://<website>');

if ( $response->is_success ) {
    print $response->decoded_content;
}
else {
    die $response->status_line;
}

See LWP::Protocol::https and LWP::UserAgent.

ikegami
  • 367,544
  • 15
  • 269
  • 518
Kenosis
  • 6,196
  • 1
  • 16
  • 16
  • 1
    Also see this: http://stackoverflow.com/questions/13385846/scripts-broke-after-upgrading-lwp-certificate-verify-failed – Richard Huxton Feb 03 '14 at 18:55
  • @CircuitB0T - As a clarification, did it work for you when `verify_hostname => 0` or `verify_hostname => 1`? – Kenosis Feb 03 '14 at 23:19
0

The reason $ua->ssl_opts( verify_hostnames => 0 ); fails is probably because you misspelled verify_hostname.

I don't know why $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; failed, but it might because the environment var has to be set before the SSL library is loaded.

ikegami
  • 367,544
  • 15
  • 269
  • 518