handeling cookies and headers with agilitypack C#

.net c# html-agility-pack

Question

agilitypack does excellent job for me in html parsing, but what about other html aspects? the object agilitypack.HtmlWeb allow access to cookies and headers?

Popular Answer

Access to the cookies and headers could be obtained through HtmlWeb.PreRequest and HtmlWeb.PostResponse handlers. The first one occurs before an HTTP request is executed. The second one occurs after an HTTP request has been executed. To use cookies you should enable it for an HtmlWeb instance by setting HtmlWeb.UseCookies property to true.

Here is an example:

var web = new HtmlWeb { UseCookies = true };
web.PreRequest += request =>
{
    // gets access to the cookie container
    var cookieContainer = request.CookieContainer;
    //  gets access to the request headers
    var headers = request.Headers;
    return true;
};
web.PostResponse += (request, response) =>
{
    // response headers
    var headers = response.Headers;
    // cookies
    var cookies = response.Cookies;
};



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