开发者

WWW::Mechanize Perl login only works after relaunch

开发者 https://www.devze.com 2022-12-24 03:18 出处:网络
I\'m trying to login automatically in a website using Perl with WWW::Mechanize. What I do is: $bot = WWW::Mechanize->new();

I'm trying to login automatically in a website using Perl with WWW::Mechanize.

What I do is:

$bot = WWW::Mechanize->new();
$bot->cookie_jar(
        HTTP::Cookies->new(
            file           => "cookies.txt",
            autosave       => 1,
            ignore_discard => 1,
        )
);

$response = $bot->get( 'http://blah.foo/login' );

$bot->form_number(1);

$bot->field( usern => 'user' );
$bot->field( pass => 'pass' );
$response =$bot->click();

print $response->content();

$response = $bot->get( 'http://blah.foo' );

print $response->开发者_开发百科;content();

The login works, but when I load the page it tells me that I am not connected.

You see that I store cookies in a file. Now if I relaunch the script without the login part, it says that I am connected...

Does anyone understand this strange behaviour ?

Edit: In fact I noticed that the problem happens too with some web browsers on certain platform. The page says "Not logged in". However, it is sufficient to reload the page to be logged in.

In the script, I tried to do a double get, but it doesn't work better. The only way is to launch it twice.

It worked with curl when I did the last request twice.


Some websites I have seen don't set or handle their session cookies correctly on every page so they fail if you access their pages in "unexpected" order. For example, the login page or login handler page or some popup content page may expect to see the session cookie already set by a normal page from the site.

This sounds like your problem, because it works the second time when the cookie is already set when you fetch the page.

I have worked around this kind of problems by simulating a more typical browser user session activity in my script by fetching some "normal" pages before going to the actual login part:

$www->get('http://www.example.com');         # Homepage
$www->get('http://www.example.com/account'); # Authenticated section front page
# Now everything is set up, proceed with account login...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号