cURL / Mailing Lists / curl-users / Single Mail

curl-users

Re: automating authentication...

From: Philippe Paclet <paclet_at_dada.it>
Date: Wed, 04 May 2005 15:53:31 +0200

If you want a perlbase solution, I would recommand the lwp module.

Here is a little snippet of code:

use strict;

use LWP::UserAgent;
use HTTP::Cookies;

my $ua = new LWP::UserAgent;
$ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0');
$ua->cookie_jar(HTTP::Cookies->new(file => "$ENV{HOME}/.cookies.txt"));
# this will handle for you the correct cookies transaction
$ua->timeout(300);

my $url = 'the intial url to which you point your browser';

my $req = new HTTP::Request GET => $baseurl;
my $res = $ua->simple_request($req);

# $res->content now contains the html code of the first page; might
be empty if redirection has been made at header level ( Location : newurl)

my $urlredirect = 'the redirected login page'; # you might have to
analyse the precedent content (or header) in caseof a dynamical redirect
page.
$req = new HTTP::Request GET => $urlredirect;
$res = $ua->simple_request($req);

# $res->content now contains the html code of the login page; check
carefully the names given to the fields of the form; check if the form
# contains any hidden fields; check also if they are some Javascript
functions which before submitting the form translate any input or hidden
values.

# Submit your login values

$urlaction = ' the action of the form',
$res = $ua->request(POST $urlaction,
                    Content_Type => 'application/x-www-form-urlencoded',
                    Content => [
                                      username => 'xxxx',
                                      password => 'yyyy' # assuming
fields have this names,and no hidden fields
                                ]
             );

# $res->content should now contain the html code of the page after the
login

Hope this will help.

Philippe Paclet

Girish Keswani wrote:

> I am totally new to cURL. But would it be possible for you to let me
> know if I can automate the following process.
>
> The whole objective is to be able to download a couple of web-pages
> behind the login screen.
>
> Usually what I do is:
> - specify the URL
> - this redirects me to authentication screen and asks me for user-name
> and password, I have to manually enter this
> - once the authentication is done, it takes me to the page of interest.
>
> I want to automate the complete process... and the manual
> authentication is totally screwing this up. Is there a way to be able
> to automate this completely using CURL or any other scheme? Please let
> me know on this.
>
> I will prefer perl based solution.
>
> thanks,
> -girish
Received on 2005-05-04