public CookieValue SetValue(string cookieId, string key, string value) { // If there's an existing CookieGuid that matches this cookieId, let's use that one. CookieValue cookieValue = CookieValues.FirstOrDefault(cv => string.Compare(cv.CookieId, cookieId, comp) == 0); Guid cookieGuid = cookieValue == null?Guid.NewGuid() : cookieValue.CookieGuid; return(SetValue(cookieGuid, cookieId, key, value)); }
public CookieValue SetValue(Guid cookieGuid, string cookieId, string key, string value) { var cookieValue = CookieValues.FirstOrDefault(cv => cv.CookieGuid == cookieGuid && string.Compare(cv.Key, key, comp) == 0); if (cookieValue == null) { cookieValue = new CookieValue { CookieGuid = cookieGuid, CookieId = cookieId, Key = key }; CookieValues.Add(cookieValue); } cookieValue.Value = value; return(cookieValue); }
private void Initialize(string mergedCookies) { CookieValues = new List <CookieValue>(); string[] cookies = mergedCookies.Split(';'); // Look through each cookie, retrieve all potential values and place them in the CookieValues list. foreach (string cookie in cookies) { int equalsPos = cookie.IndexOf('='); Guid cookieGuid = Guid.NewGuid(); // If there's no equals, use the whole string as the identifier. if (equalsPos <= 0) { var cookieValue = new CookieValue { CookieGuid = Guid.NewGuid(), CookieId = HttpUtility.UrlDecode(cookie.Trim()) }; CookieValues.Add(cookieValue); continue; } string cookieId = HttpUtility.UrlDecode(cookie.Substring(0, equalsPos).Trim()); string cookieData = cookie.Remove(0, equalsPos + 1).Trim(); string[] keyValuePairs = cookieData.Split('&'); foreach (string entry in keyValuePairs) { string[] keyValuePair = entry.Split('='); var cookieValue = new CookieValue { CookieId = cookieId, CookieGuid = cookieGuid }; if (keyValuePair.Length >= 2) { cookieValue.Key = HttpUtility.UrlDecode(keyValuePair[0].Trim()); cookieValue.Value = HttpUtility.UrlDecode(keyValuePair[1].Trim()); } else { cookieValue.Key = HttpUtility.UrlDecode(entry); } CookieValues.Add(cookieValue); } } }
internal void SetCookie() { if (!URI.IsNullOrEmpty() && Cookies != null && Cookies.Count > 0) { Cookies.ForDicEach((key, val) => { OptionBuilder.Cookies.Add(new Uri(URI), new Cookie(key, val)); }); } if (!URI.IsNullOrEmpty() && CookieColl != null && CookieColl.Count > 0) { OptionBuilder.Cookies.Add(new Uri(URI), CookieColl); } if (!CookieName.IsNullOrEmpty() && !CookieValue.IsNullOrEmpty() && !CookiePath.IsNullOrEmpty()) { OptionBuilder.Cookies.Add(new Cookie(CookieName, CookieValue, CookiePath, CookieDomain)); } }
internal CookieContainer SetCookie(CookieContainer Container) { return(SyncStatic.TryCatch(() => { if (Container == null) { Container = new CookieContainer(); } if (InstanceCookie) { return Container; } else { if (!URI.IsNullOrEmpty() && Cookies != null && Cookies.Count > 0) { Cookies.ForDicEach((key, val) => { Container.Add(new Uri(URI), new Cookie(key, val)); }); return Container; } else if (!URI.IsNullOrEmpty() && CookieColl != null && CookieColl.Count > 0) { Container.Add(new Uri(URI), CookieColl); return Container; } else if (!CookieName.IsNullOrEmpty() && !CookieValue.IsNullOrEmpty() && !CookiePath.IsNullOrEmpty()) { Container.Add(new Cookie(CookieName, CookieValue, CookiePath, CookieDomain)); return Container; } else { throw new Exception("Cookie配置不满足!"); } } }, ex => throw ex)); }
public void SetUp() { _cookieValue = new CookieValue("name", "value"); }