工作中的内部网站托管在与大多数内部网站不同的服务器上。该站点输出一些我想通过屏幕抓取获得的信息。我在其他内部网站上使用asp.net(C#)页面和HTTPWebRequest进行了屏幕抓取,但与大多数情况不同,此网站需要用户名和密码。用户名和密码不是秘密,它们是沿着登录页面,每个人都使用相同的登录信息。
我在网上看到了一些完成自动登录的例子,但这些都不是我需要的。我想使用aspx页面登录该站点并从下一页检索一些数据。
我见过的例子涉及生成cookie并将登录数据发布到HTTPWebRequest Stream。在这种情况下,我真的不确定如何做到这一点。
是否可以简单地填充表单域并执行submitw按钮(以编程方式和幕后)。
以下是登录页面的部分代码:
<script>
//StartTranslate:NetLanguage
function window_onload() {
deleteCookie("BodyURL","/Net",0);
document.loginform.UserName.focus();
document.loginform.UserName.value=sUserName;
document.loginform.UserName.select();
}
function doSubmit() {
var sUserName = SMCookieGetUserName();
loginform.submit();
}
</script>
<form name="loginform" action="/Net//netportal.dll/SubmitLogin" method="post" >
<input class="textbox" type="text" name="UserName" id="UserName" maxlength="128" tabindex="1" >
<input class="textbox" type="password" name="Password" id="Password" maxlength="128" tabindex="2" >
<img onClick="doSubmit();" src='/net/PortalPages/Images/slogin.gif' onselectstart="return false;" tabindex="3">
<input type="hidden" value="" name="Timezone">
<input type="hidden" value="" name="redirect">
<input type="hidden" value="true" name="ExplicitLogin">
</form>
答案 0 :(得分:0)
我认为使用这样的应用程序你只需要将浏览器直接调用到服务器,而不是试图弄乱html。您可以将预期的表单值发布到操作URL,它应该可以工作....
所以在您的代码中只需调用/Net//netportal.dll/SubmitLogin并添加隐藏字段,UserName和Password,然后在服务器登录后开始抓取。这里有一些代码示例可以用来开始...只是改变一下。您也可以考虑使用htmlagilitypack http://htmlagilitypack.codeplex.com/
private static string Post ( string Url, string Method, string Content, string ContentType = "application/json", WebHeaderCollection headers = null )
{
var address = new Uri(Url);
var request = WebRequest.Create(address) as HttpWebRequest;
request.Method = Method;
if (headers != null)
request.Headers.Add(headers);
if (!String.IsNullOrEmpty(Content))
{
var bytes = Encoding.UTF8.GetBytes(Content);
request.ContentLength = bytes.Length;
request.ContentType = ContentType;
using (var pStream = request.GetRequestStream())
{
pStream.Write(bytes, 0, bytes.Length);
}
}
using (var response = request.GetResponse() as HttpWebResponse)
{
var reader = new StreamReader(response.GetResponseStream());
return reader.ReadToEnd();
}
}