Basic Authorization and Cookies
am 02.12.2004 03:01:21 von psheth
Hello:
I'm able to access a web page using basic authorization. However, when I
click a link to go to the next page (without basic authorization), I get
an 'Unauthorized' error message. I believe its something to do with
cookies which I tried implementing but in vain. Following is the code
snippet that I'm trying:
$req = HTTP::Request->new(GET => "$url");
$req->authorization_basic("$browser_username", "$browser_password");
$cookie_jar = HTTP::Cookies->new(file => "cookies.txt",autosave => 1,);
$cookie_jar->load("cookies.txt");
$cookie_jar->add_cookie_header($req);
$mech = WWW::Mechanize->new( autocheck => 1 );
$mech->cookie_jar($cookie_jar);
$res = $mech->request($req);
$cookie_jar->extract_cookies( $res );
$cookie_jar->save("cookies.txt");
$cookie_jar->load("cookies.txt");
$mech->follow_link( text => "Click This Link" );
I can go to "Click This Link" if $req is once again created with GET and
new URL and authorization_basic() is called with username and password
followed by $mech->request($req).
Can someone please point out where and what am I doing wrong or is there
a better way to do this?
Thanks,
-Pritesh
Re: Basic Authorization and Cookies
am 03.12.2004 03:00:59 von psheth
Figured out the problem - was using libwww-perl-5.76 version...I should
have realized this when default_headers() was not found for User-Agent.
Instead I continued using HTML::Form (instead of Mechanize) and parse()
couldn't find decoded_content(). Got the new version and am able to
perform the required operation by adding the basic authorization to the
request header:
my $mech = WWW::Mechanize->new( autocheck => 1 );
$mech->default_headers->authorization_basic("$browser_userna me",
"$browser_password");
$mech->get($url);
Also no longer have to use HTML::Form. $mech->submit_form(...) works fine.
-Pritesh
> Hello:
>
> I'm able to access a web page using basic authorization. However, when I
> click a link to go to the next page (without basic authorization), I get
> an 'Unauthorized' error message. I believe its something to do with
> cookies which I tried implementing but in vain. Following is the code
> snippet that I'm trying:
>
> $req = HTTP::Request->new(GET => "$url");
> $req->authorization_basic("$browser_username", "$browser_password");
> $cookie_jar = HTTP::Cookies->new(file => "cookies.txt",autosave => 1,);
> $cookie_jar->load("cookies.txt");
> $cookie_jar->add_cookie_header($req);
> $mech = WWW::Mechanize->new( autocheck => 1 );
> $mech->cookie_jar($cookie_jar);
> $res = $mech->request($req);
> $cookie_jar->extract_cookies( $res );
> $cookie_jar->save("cookies.txt");
> $cookie_jar->load("cookies.txt");
> $mech->follow_link( text => "Click This Link" );
>
> I can go to "Click This Link" if $req is once again created with GET and
> new URL and authorization_basic() is called with username and password
> followed by $mech->request($req).
>
> Can someone please point out where and what am I doing wrong or is there
> a better way to do this?
>
> Thanks,
>
> -Pritesh
>
>
>
>