问题
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