mclafee
mclafee

Reputation: 1426

handeling cookies and headers with agilitypack C#

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?

Upvotes: 4

Views: 2467

Answers (1)

Oleks
Oleks

Reputation: 32323

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

Upvotes: 5

Related Questions