/// <summary> /// 将cookie加入singlecookie的List中 并剔除path expires domain /// </summary> /// <param name="Cookie">传入的String 类型Cookie</param> /// <returns></returns> private List <SingleCookie> CreatCookieList(string Cookie) { List <String> StringArray = new List <string>(); var arr1 = Cookie.Split(';'); if (arr1.Length > 0) { foreach (var cookieline in arr1) { var arr2 = cookieline.Split(','); if (arr2.Length > 0) { foreach (var scookie in arr2) { var arr3 = scookie.Split('='); if (arr3.Length == 2 && !arr3[0].ToLower().Contains("httponly") && !arr3[0].ToLower().Contains("path") && !arr3[0].ToLower().Contains("expires") && !arr3[0].ToLower().Contains("domain")) { StringArray.Add(scookie); } } } } } List <SingleCookie> Cookielist = new List <SingleCookie>(); try { String[] SingleArray; StringArray.ToList().ForEach(singlecookiestr => { //以=为界分割 SingleArray = singlecookiestr.Split('='); //将cookie名称转化为小写并trim() String cookiename = SingleArray[0].ToLower().Replace(" ", String.Empty).Trim(); //单个数组长度 int length = SingleArray.Length; if (cookiename != String.Empty && length == 2) { //构造cookie SingleCookie sc = new SingleCookie(SingleArray); //Add Cookielist.Add(sc); } }); } catch { } return(Cookielist); }
/// <summary> /// 将cookie加入singlecookie的List中 并剔除path expires domain /// </summary> /// <param name="Cookie">传入的String 类型Cookie</param> /// <returns></returns> private List <SingleCookie> CreatCookieList(string Cookie) { List <SingleCookie> Cookielist = new List <SingleCookie>(); try { //去除 cookie字符串中的httponly path expires Regex httponlyreg = new Regex("httponly(,|/)*", RegexOptions.IgnoreCase); Regex pathreg = new Regex("path=/(,|/)*", RegexOptions.IgnoreCase); Regex expiresreg = new Regex("expires(,|/)*", RegexOptions.IgnoreCase); Cookie = httponlyreg.Replace(Cookie, String.Empty); Cookie = pathreg.Replace(Cookie, String.Empty); Cookie = expiresreg.Replace(Cookie, String.Empty); Cookie = Cookie.Replace(" ", String.Empty); //以;为界分割 String[] StringArray = Cookie.Split(';'); String[] SingleArray; StringArray.ToList().ForEach(singlecookiestr => { //以=为界分割 SingleArray = singlecookiestr.Split('='); //将cookie名称转化为小写并trim() String cookiename = SingleArray[0].ToLower().Replace(" ", String.Empty).Trim(); //单个数组长度 int length = SingleArray.Length; if (cookiename != String.Empty && length == 2) { //构造cookie SingleCookie sc = new SingleCookie(SingleArray); //Add Cookielist.Add(sc); } }); //Parallel.ForEach(StringArray, singlecookiestr => //{ //}); } catch { } return(Cookielist); }