handeling cookies and headers with agilitypack C#

允我心安 提交于 2020-01-01 19:09:04

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/11134558/handeling-cookies-and-headers-with-agilitypack-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!