Htmlagilitypack after login to Https Website with HttpWebRequest?

c# html-agility-pack https httpwebrequest

Question

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 : Pluralsight) and get response and parse with Htmlagility pack.

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

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

Popular Answer

You have to get the stream for the response via HttpWebResponse.GetResponseStream and then load the document via the Load method of the HtmlDocument.

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


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why