欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 爬虫----Cookies处理(Set-Cookie)

程序员文章站 2022-10-06 19:58:54
/// /// Cookie 助手 /// public class CookieHelper { /// /// 解析Cookie /// private static readonly Regex RegexSplitCookie2 = new Regex(@"[^,][\S\s]... ......
    /// <summary>
    /// cookie 助手
    /// </summary>
    public class cookiehelper
    {
        /// <summary>
        /// 解析cookie
        /// </summary>
        private static readonly regex regexsplitcookie2 = new regex(@"[^,][\s\s]+?;+[\s\s]+?(?=,\s)");

        /// <summary>
        /// 获取所有cookie 通过set-cookie
        /// </summary>
        /// <param name="setcookie"></param>
        /// <returns></returns>
        public static cookiecollection getcookiesfromheader(string setcookie)
        {
            var cookiecollection = new cookiecollection();
            //拆分cookie
            setcookie += ",t";//配合regexsplitcookie2 加入后缀
            var liststr = regexsplitcookie2.matches(setcookie);
            //循环遍历
            foreach (match item in liststr)
            {
                //根据; 拆分cookie 内容
                var cookieitem = item.value.split(';');
                var cookie = new cookie();
                for (var index = 0; index < cookieitem.length; index++)
                {
                    var info = cookieitem[index];
                    //第一个 默认 cookie name
                    //判断键值对
                    if (info.contains("="))
                    {
                        var indexk = info.indexof('=');
                        var name = info.substring(0, indexk).trim();
                        var val = info.substring(indexk + 1);
                        if (index == 0)
                        {
                            cookie.name = name;
                            cookie.value = val;
                            continue;
                        }
                        if (name.equals("domain", stringcomparison.ordinalignorecase))
                        {
                            cookie.domain = val;
                        }
                        else if (name.equals("expires", stringcomparison.ordinalignorecase))
                        {
                            datetime.tryparse(val, out var expires);
                            cookie.expires = expires;
                        }
                        else if (name.equals("path", stringcomparison.ordinalignorecase))
                        {
                            cookie.path = val;
                        }
                        else if (name.equals("version", stringcomparison.ordinalignorecase))
                        {
                            cookie.version = convert.toint32(val);
                        }
                    }
                    else
                    {
                        if (info.trim().equals("httponly", stringcomparison.ordinalignorecase))
                        {
                            cookie.httponly = true;
                        }
                    }
                }
                cookiecollection.add(cookie);
            }
            return cookiecollection;
        }

        /// <summary>
        /// 获取 cookies
        /// </summary>
        /// <param name="setcookie"></param>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static string getcookies(string setcookie, uri uri)
        {
            //获取所有cookie
            var strcookies = string.empty;
            var cookies = getcookiesfromheader(setcookie);
            foreach (cookie cookie in cookies)
            {
                //忽略过期cookie
                if (cookie.expires < datetime.now && cookie.expires != datetime.minvalue)
                {
                    continue;
                }
                if (uri.host.contains(cookie.domain))
                {
                    strcookies += $"{cookie.name}={cookie.value}; ";
                }
            }
            return strcookies;
        }
    }