带有隐藏输入的 HttpWebRequest 登录



我正在尝试在Windows 8应用商店应用程序中使用HttpWebRequest登录网站。登录表单如下所示:

<form method="post" action="" onsubmit="javascript:return FormSubmit();">
<div>
    <div>
        <span>gebruikersnaam</span>
        <input size="17" type="text" id="username" name="username" tabindex="1" accesskey="g" />
    </div>
    <div>
        <span>wachtwoord</span>
        <input size="17" type="password" id="password" name="password" maxlength="48" tabindex="2" accesskey="w" autocomplete="off" />
    </div>
    <div class="controls">
        <input type="submit" id="submit" accesskey="l" value="inloggen" tabindex="4" class="button"/>
    </div>
    <!-- The following hidden field must be part of the submitted Form -->
    <input type="hidden" name="lt" value="_s3E91853A-222D-76B6-16F9-DB4D1FD397B7_c8424159E-BFAB-EA2A-0576-CD5058A579B4" />
    <input type="hidden" name="_eventId" value="submit" />
    <input type="hidden" name="credentialsType" value="ldap" />
</div>
</form>

我能够发送大部分必需的输入,除了名为"lt"的隐藏输入。出于安全目的,这是一个随机生成的代码,因此我无法在脚本中对其进行硬编码。我目前的脚本是这样的:

HttpWebRequest loginRequest2 = (HttpWebRequest)WebRequest.Create(LOGIN_URL_REDIRECT);
        loginRequest2.CookieContainer = CC;
        loginRequest2.Method = "POST";
        loginRequest2.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*";
        loginRequest2.ContentType = "application/x-www-form-urlencoded";
        loginRequest2.Headers["Accept-Encoding"] = "gzip,deflate";
        loginRequest2.Headers["Accept-Language"] = "en-us";
        StreamWriter sw = new StreamWriter(await loginRequest2.GetRequestStreamAsync());
        sw.Write("username=" + userName + "&password=" + passWord + "&_eventId=submit&credentialsType=ldap");
        await sw.FlushAsync();
        HttpWebResponse response2 = (HttpWebResponse)await loginRequest2.GetResponseAsync();

在执行请求之前,如何获取隐藏输入"lt"的内容?

使用 HTML Agilty Pack,您可以使用以下代码片段获取 Hidden Field 值:

var doc = new HtmlWeb().Load(LOGIN_URL_REDIRECT);
var nodes = doc.DocumentNode
               .SelectNodes("//input[@type='hidden' and @name='lt' and @value]");
foreach (var node in nodes) {
    var inputName = node.Attributes["name"].Value;
    var inputValue = node.Attributes["value"].Value;
    Console.WriteLine("Name: {0}, Value: {1}", inputName, inputValue);
}

使用 Linq:

var nodes = from n in doc.DocumentNode.DescendantNodes()
            where n.Name == "input" && 
                  n.GetAttributeValue("type", "") != "" &&
                  n.GetAttributeValue("name", "") == "lt" && 
                  n.Attributes.Contains("value")
            select new
            {
                n.Attributes["name"].Name,
                n.Attributes["value"].Value
            };
foreach (var node in nodes) {
    Console.WriteLine("Name: {0}, Value: {1}", node.Name, node.Value);
}
您需要

加载网页并获取随机密钥并使用相同的密钥调用第二页,我编写了一个可以轻松完成此操作的软件,请在此处阅读 http://innosia.com/Home/Article/WEBSCRAPER

如果你不想使用WebScraper,至少你需要使用Cookie ware类,这是在下载的解决方案中找到的CookieWebClient,使用它应该是这样的:

// Use Cookie aware class, this class can be found in my WebScraper Solution
CookieWebClient cwc = new CookieWebClient;
string page = cwc.DownloadString("http://YourUrl.com"); // Cookie is set to the key
// Filter the key
string search = "name="lt" value="";
int start  = page.IndexOf(search);
int end = page.IndexOf(""", start);
string key = page.Substring(start + search.Length, end-start-search.Length);
// Use special method in CookieWebClient to post data since .NET implementation has some issues.
// CookieWebClient is the class I wrote found in WebScraper solution you can download from my site
// Re use the cookie that is set to the key
string afterloginpage = cwc.PostPage("http://YourUrl.com", string.Format("username={0}&password={1}&lt={2}&_eventId=submit&credentialsType=ldap", userid, password, key));
// DONE

相关内容

  • 没有找到相关文章

最新更新