2012-11-17 65 views
3

我試圖在Windows 8商店應用程序中使用HttpWebRequest登錄網站。登錄形式如下: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」的te內容?

回答

0

使用HTML Agilty Pack,您可以使用此代碼段來獲取隱藏字段值:

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); 
} 
0

你需要加載網頁並獲得隨機密鑰並使用相同的密鑰來呼叫第二頁,我寫了一個軟件,可以很容易地做到這一點,在這裏閱讀http://innosia.com/Home/Article/WEBSCRAPPER

如果你不想使用WebScrapper,你至少需要使用Cookie識別類是CookieWebClient在下載的解決方案中發現,使用它應該是這樣的:

// Use Cookie aware class, this class can be found in my WebScrapper 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 WebScrapper 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