c# - 使用 agility pack C# 处理 cookie 和 header

标签 c# .net html-agility-pack

agilitypack 在 html 解析方面对我来说做得很好, 但是其他 html 方面呢? 对象 agilitypack.HtmlWeb 是否允许访问 cookie 和 header ?

最佳答案

可以通过 HtmlWeb.PreRequestHtmlWeb.PostResponse 处理程序访问 cookie 和 header 。第一个发生在执行 HTTP 请求之前。第二个发生在执行 HTTP 请求之后。要使用 cookie,您应该通过将 HtmlWeb.UseCookies 属性设置为 trueHtmlWeb 实例启用它。

这是一个例子:

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

关于c# - 使用 agility pack C# 处理 cookie 和 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11134558/

相关文章:

c# - 从多个接口(interface)继承相同的方法

c# - 转换 asp :Table to excel format

java - 即时编译器和解释器有什么区别?

c# - C# 中 HtmlDocument 引用的 NullReferenceException

c# - 使用 HtmlAgilityPack 从 WP7 上的 HTML 获取文本

c# - 使用 HtmlAgilityPack 获取 iframe 源

c# - Mongo 更新数组元素(.NET 驱动程序 2.0)

c# - 如何在C#中访问JS变量

c# - 获取编译时间

c# - 在 xml 序列化期间忽略属性,但在反序列化期间不忽略