uni_j has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering why WWW::Mechanize won’t accept the cookies for this site https://utdirect.utexas.edu/registrar/nrclav/results.WBX. I’ve turned it inside out trying to figure out why it gives me an error. “Cookies must be enabled to logon with your UT EID and password. Please change your browser settings to accept cookies if you wish to connect to this system.” Technically the submit form functions in WWW should work ...

Replies are listed 'Best First'.
Re: WWW:Mechanize and Cookies
by perrin (Chancellor) on Mar 31, 2009 at 20:46 UTC
    Try this:
    use HTTP::Cookies; $agent->cookie_jar(HTTP::Cookies->new);
      WWW::Mechanize creates a cookie_jar like that by default.
        It hasn't always done that, and there could be a problem with it. Doing it explicitly helps debug.
Re: WWW:Mechanize and Cookies
by Gangabass (Vicar) on Apr 01, 2009 at 00:26 UTC

    May be this is ignore_discard?

    use HTTP::Cookies; my $cookie_jar = HTTP::Cookies->new( ignore_discard => 1 ); $mech->cookie_jar($cookie_jar);
      That would only affect saving cookies to a file, like after the object is destroyed (like when a program ends, between sessions).
Re: WWW:Mechanize and Cookies
by Gangabass (Vicar) on Apr 01, 2009 at 02:34 UTC

    This work for me:

    #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->get("https://utdirect.utexas.edu/registrar/nrclav/results.WBX") +; $mech->field( "PASSWORDS", "pass", 2 ); $mech->submit_form( form_name => "logonform", fields => { LOGON => "rc32536", }, ); $mech->get("https://utdirect.utexas.edu/registrar/nrclav/results.WBX") +;

    Second get needed because of JavaScript redirect to this URL on the response page.

Re: WWW:Mechanize and Cookies
by Anonymous Monk on Apr 01, 2009 at 02:27 UTC
    A poor workman blames his tools.
    #!/usr/bin/perl -- use strict; use warnings; use WWW::Mechanize; my $ua = WWW::Mechanize->new; $ua->get(qw! https://utdirect.utexas.edu/registrar/nrclav !); print "\n\n", $ua->response->status_line, " ", $ua->uri, "\n\n", $ua->cookie_jar->as_string, "\n\n"; $ua->set_visible(qw' TESTusername TESTpassword'); print $ua->current_form->dump,"\n\n"; $ua->submit; print "\n\n", $ua->response->request->as_string, "\n\n", $ua->cookie_jar->as_string, "\n\n"; print "Error: $1 " if $ua->content =~ m{\Q<span class="error">\E(.+?)\ +Q</span>\E}s; __END__ 200 OK https://utdirect.utexas.edu/registrar/nrclav/index.WBX Set-Cookie3: DOC="/registrar/nrclav/index.WBX"; path="/"; domain=.utex +as.edu; path_spec; discard; version=0 Set-Cookie3: FC=NONE; path="/"; domain=.utexas.edu; path_spec; discard +; version=0 Set-Cookie3: PDLK=NONE; path="/"; domain=.utexas.edu; path_spec; disca +rd; version=0 Set-Cookie3: SC=NONE; path="/"; domain=.utexas.edu; path_spec; discard +; version=0 POST https://utdirect.utexas.edu/security-443/logon_check.logonform [l +ogonform] CDT=20090331212639 (hidden readonly) NEW_PASSWORD= (hidden readonly) CONFIRM_NEW_PASSWORD= (hidden readonly) PASSWORDS= (hidden readonly) LOGON=TESTusername (text) PASSWORDS=TESTpassword (password) <NONAME>=Log In (submit) POST https://utdirect.utexas.edu/security-443/logon_check.logonform Accept-Encoding: gzip Referer: https://utdirect.utexas.edu/registrar/nrclav User-Agent: WWW-Mechanize/1.54 Content-Length: 107 Content-Type: application/x-www-form-urlencoded Cookie: SC=NONE; PDLK=NONE; FC=NONE; DOC=/registrar/nrclav/index.WBX Cookie2: $Version="1" CDT=20090331212639&NEW_PASSWORD=&CONFIRM_NEW_PASSWORD=&PASSWORDS=&LOGO +N=TESTusername&PASSWORDS=TESTpassword Set-Cookie3: DOC="/registrar/nrclav/index.WBX"; path="/"; domain=.utex +as.edu; path_spec; discard; version=0 Set-Cookie3: FC=NONE; path="/"; domain=.utexas.edu; path_spec; discard +; version=0 Set-Cookie3: PDLK=NONE; path="/"; domain=.utexas.edu; path_spec; disca +rd; version=0 Set-Cookie3: SC=NONE; path="/"; domain=.utexas.edu; path_spec; discard +; version=0 Set-Cookie3: TS=20090331212639; path="/"; domain=.utexas.edu; path_spe +c; discard; version=0 Error: An error has occurred. You are not at fault. This error has +automatically been reported to the System Administrator. Please try a +gain later.