表单身份验证。使用Perl的.NET站点

时间:2015-04-22 06:36:23

标签: forms perl authentication mechanize lwp

我们是Perl脚本的新手,我们希望通过.NET站点身份验证。 我们有一个针对Sharepoint网站的现有身份验证代码,代码段如下:

my $ua = new LWP::UserAgent('keep_alive' => '1');
print "$url \n";
$ua->credentials("$uname", "$pwd");
my $response =$ua->get($url);
my $cde = $response->code();

我们希望自定义代码,以便它特定于.NET站点身份验证。我们使用了WWW :: Mechanize,请参阅下面的代码片段:

my $mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->form_name('Login');
$mech->field("UserName", $username);
$mech->field("Password", $password);
$mech->click();

但是当程序运行时,会显示错误:"没有名为' Login'在sample.pl"。有关如何验证特定于.NET站点的表单的任何想法吗?

添加:HTML表单代码段...

<div id="loginArea">        
        <div id="loginMessage" class="groupMargin">Sign in with your account</div>
        <form method="post" id="Login" autocomplete="off" novalidate="novalidate" onkeypress="if (event &amp;&amp; event.keyCode
== 13) Login.submitLoginRequest();" action="">
            <div id="formsAuthenticationArea">
                <div id="userNameArea">
                    <input id="userNameInput" name="UserName" type="email" value="" tabindex="1" class="text fullWidth" spellcheck="false" placeholder="Username" autocomplete="off">     
                </div>
                <div id="passwordArea">
                     <input id="passwordInput" name="Password" type="password" tabindex="2" class="text fullWidth" placeholder="Password" autocomplete="off">                            

                </div>
                <div id="kmsiArea" style="display:none">
                    <input type="checkbox" name="Kmsi" id="kmsiInput" value="true" tabindex="3">
                    <label for="kmsiInput">Keep me signed in</label>
                </div>
                <div id="submissionArea" class="submitMargin">
                    <span id="submitButton" class="submit" tabindex="4" onkeypress="if (event &amp;&amp; event.keyCode == 32) Login.submitLoginRequest();" onclick="return Login.submitLoginRequest();">Sign in</span>
                </div>
            </div>
        </form>
    </div>

1 个答案:

答案 0 :(得分:0)

通常此代码适用于.NET站点:

$mech->submit_form(
    with_fields => {
        login => "somelogin",
        pass  => "somepass",
    },
    button => "submit_button_name",
);

但是你有一个奇怪的错误信息:你正在选择&#34;登录&#34;表格但$mech正在搜索&#34; loginForm&#34; ...您能否显示实际表单的HTML?

此外,无需执行$mech->cookie_jar(...),因为$mech默认执行此操作。

<强>更新 试试这段代码(Loginid表单,而不是name):

$mech->submit_form(
    with_fields => {
        UserName => "somelogin",
        Password  => "somepass",
    },
);