How can I Browse a page Programmatically?

c# html-agility-pack

Question

I've seen numerous examples on how to get the contents of a URI. I also used HTMLAgilityPack a lot. What I want is to create Unit Testing environment for asp websites.

I've seen the BrowserSession and this Question but although, the process seems fine, they do not login in a website. I tried numerous well-known websites.

Any ideas on how to browse though code?

Popular Answer

It sounds like you want to submit a form on a web page and view the response HTML back of the resulting page.

This method will take a form target URL and submit a post with the given named arguments in the parms Dictionary.

I have used the method below to perform password authentication on a web page and view the response after authentication. You will need to know the target Url and the form fields you wish to pass in the request.

private string SubmitRequest(string url, Dictionary<string, string> parms)
{
    var req = WebRequest.Create(url);
    req.Method = "POST";

    string parmsString = string.Join("&", parms.Select(p => string.Format("{0}={1}", p.Key, p.Value)));
    req.ContentLength = parmsString.Length;

    using (StreamWriter writer = new StreamWriter(req.GetRequestStream()))
    {
        writer.Write(parmsString);
        writer.Close();
    }

    var res = req.GetResponse();

    using (StreamReader reader = new StreamReader(res.GetResponseStream()))
    {
        string response = reader.ReadToEnd();
        reader.Close();
        return response;
    }
}

If there is something more specific you are wanting or this is not what you are looking for then please post a comment.




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