2016-05-23 81 views
2

我想解析一些html網站like pluralsight,例如(https://app.pluralsight.com/id?),那麼我怎樣才能首先登錄網站programmaticaly(不使用webbrowser控制),然後調用另一個url(例如:Pluralsight),並得到響應和解析與hlmlagility包。Htmlagilitypack用HttpWebRequest登錄Https網站後?

但我寫了一個登錄代碼,但我不知道下一步。

public class Login 
{ 
    private CookieContainer Cookies = new CookieContainer(); 



public void SiteLogin(string username, string password) 
{ 
    Uri site = new Uri("https://app.pluralsight.com/id?"); 

    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(site); 
    wr.Method = "Post"; 
    wr.ContentType = "application/x-www-form-urlencoded"; 
    wr.Referer = "https://app.pluralsight.com/id?"; 
    wr.CookieContainer = Cookies; 
    var parameters = new Dictionary<string, string>{ 
    {"realm", "vzw"}, 
    {"goto",""}, 
    {"gotoOnFail",""}, 
    {"gx_charset", "UTF-8"}, 
    {"rememberUserNameCheckBoxExists","Y"}, 
    {"IDToken1", username}, 
    {"IDToken2", password} 
}; 

    string input = string.Empty; 
    using (var requestStream = wr.GetRequestStream()) 
    using (var writer = new StreamWriter(requestStream, Encoding.UTF8)) 
     writer.Write(ParamsToFormEncoded(parameters)); 

    using (var response = (HttpWebResponse)wr.GetResponse()) 
    { 

     if (response.StatusCode == HttpStatusCode.OK) 
     { 
      //but I do not know the next step. 
     } 
    } 
} 

private string ParamsToFormEncoded(Dictionary<string, string> parameters) 
{ 
    return string.Join("&", parameters.Select(kvp => Uri.EscapeDataString(kvp.Key).Replace("%20", "+") 
    + "=" + Uri.EscapeDataString(kvp.Value).Replace("20%", "+") 
    ).ToArray()); 
} 
} 

回答

0

你必須通過HttpWebResponse.GetResponseStream得到了響應流,然後通過的HTMLDocument的Load方法加載文檔。

var doc = new HtmlAgilityPack.HtmlDocument(); 
doc.Load(response.GetResponseStream()); 
//further processing... 
+0

但我想地址發送另一個請求,另一個URL(例如:https://app.pluralsight.com/library/courses/object-oriented-programming-fundamentals-csharp/table-of-內容)並用Hlmlagility包得到響應和解析。 – Matteo