behnam farhadi

behnam farhadi

  • NA
  • 158
  • 12.3k

Htmlagilitypack after login to Https Website HttpWebReqest?

May 24 2016 3:12 AM

I want to parse some html site like pluralsight,Forexample (https://app.pluralsight.com/id?), So How can I first login site programmaticaly (without using webbrowser control) then call another url (for example : https://app.pluralsight.com/library/courses/object-oriented-programming-fundamentals-csharp/table-of-contents) and get response and parse with Htmlagility pack.

But I've written a login code, but I do not know the next step.

  1. public class Login  
  2. {  
  3.     private CookieContainer Cookies = new CookieContainer();  
  4.   
  5.   
  6.   
  7. public void SiteLogin(string username, string password)  
  8. {  
  9.     Uri site = new Uri("https://app.pluralsight.com/id?");  
  10.   
  11.     HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(site);  
  12.     wr.Method = "Post";  
  13.     wr.ContentType = "application/x-www-form-urlencoded";  
  14.     wr.Referer = "https://app.pluralsight.com/id?";  
  15.     wr.CookieContainer = Cookies;  
  16.     var parameters = new Dictionary<stringstring>{  
  17.     {"realm""vzw"},  
  18.     {"goto",""},  
  19.     {"gotoOnFail",""},  
  20.     {"gx_charset""UTF-8"},  
  21.     {"rememberUserNameCheckBoxExists","Y"},  
  22.     {"IDToken1", username},  
  23.     {"IDToken2", password}  
  24. };  
  25.   
  26.     string input = string.Empty;  
  27.     using (var requestStream = wr.GetRequestStream())  
  28.     using (var writer = new StreamWriter(requestStream, Encoding.UTF8))  
  29.         writer.Write(ParamsToFormEncoded(parameters));  
  30.   
  31.     using (var response = (HttpWebResponse)wr.GetResponse())  
  32.     {  
  33.   
  34.         if (response.StatusCode == HttpStatusCode.OK)  
  35.         {  
  36.             //but I do not know the next step.  
  37.         }  
  38.     }  
  39. }  
  40.   
  41. private string ParamsToFormEncoded(Dictionary<stringstring> parameters)  
  42. {  
  43.     return string.Join("&", parameters.Select(kvp => Uri.EscapeDataString(kvp.Key).Replace("%20""+")  
  44.     + "=" + Uri.EscapeDataString(kvp.Value).Replace("20%""+")  
  45.     ).ToArray());  
  46. }  
  47. }