我们是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 && 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 && event.keyCode == 32) Login.submitLoginRequest();" onclick="return Login.submitLoginRequest();">Sign in</span>
</div>
</div>
</form>
</div>
答案 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
默认执行此操作。
<强>更新强>
试试这段代码(Login
是id
表单,而不是name
):
$mech->submit_form(
with_fields => {
UserName => "somelogin",
Password => "somepass",
},
);