• HttpWebRequest采集读取网站挂载Cookie的通用方法


    Asp.net 版本

    HttpWebRequest采集时添加:httpWebRequest.CookieContainer = new CookieContainer();就能远程挂载上cookie,那么怎样去读取挂载上的cookie呢?

    下面方法为大家解除烦恼。

    遍历方法:

    public static List<Cookie> GetAllCookies(CookieContainer cc)
    {
        List<Cookie> lstCookies = new List<Cookie>();
    
        Hashtable table = (Hashtable)cc.GetType().InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cc, new object[] { });
        StringBuilder sb = new StringBuilder();
        foreach (object pathList in table.Values)
        {
            SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, pathList, new object[] { });
            foreach (CookieCollection colCookies in lstCookieCol.Values)
                foreach (Cookie c in colCookies)
                {
                    lstCookies.Add(c);
                    sb.AppendLine(c.Domain + ":" + c.Name + "____" + c.Value + "\r\n");
                }
        }
        return lstCookies;
    }

    使用:

    List<Cookie> _cookieList = GetAllCookies(req.CookieContainer);
    string _cookieValue = _cookieList[0].ToString();
  • 相关阅读:
    二叉搜索树
    【树】List Leaves
    模板——dijkstra单源最短路
    余数求和——除法分块
    倍增——ST表
    线段树——内存池
    线段树——模板
    洛谷 P1498 南蛮图腾
    洛谷 P2199 最后的迷宫
    洛谷 P1495 中国剩余定理
  • 原文地址:https://www.cnblogs.com/vipstone/p/2716422.html
Copyright © 2020-2023  润新知