Search code examples
perllwp

Use Perl to download files from website that requires a p12 certificate


I have a website that I need to download files from. The website requires a digital certificate which I have. I have a p12 file that the site provided which is very easy to import into Windows which allows access to the site. What I want to do is download a bunch of csv files from the site and parse them.

I found the Net::SSLeay and Crypt::SSLeay packages. I also see that LWP::Useragent has mention of SSL but I'm not sure if that is sufficient for what I'm trying to do.

So basically I need to know where to get started and which packages will be the easiest to use for this purpose.
I am running windows 7 with strawberry perl 5.16.2


Solution

  • Providing you have your P12 converted into PEM format, you can use:

    use Mojo::UserAgent;
    my $url = 'https://some.site.com/path/to/file.zip';
    my $ua = Mojo::UserAgent->new( max_redirects=>5, 
                                   cert=>'client_cert.crt.pem', 
                                   key=>'client_cert.key.pem' );
    $ua->get($url)->res->content->asset->move_to('downloaded_file.zip');
    

    If you are using strawberry perl you need first to install Mojo::UserAgent module like this:

    c:\> cpan -i Mojo::UserAgent