using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WxOrder.Framework.Helpers; /// /// Cookie对象模型 /// public class CookieItem { public string Name { get; set; } public string Value { get; set; } public string Domain { get; set; } public string Path { get; set; } public bool Secure { get; set; } public bool HttpOnly { get; set; } public DateTime? Expires { get; set; } public DateTime? Creation { get; set; } public DateTime? LastAccess { get; set; } public int SameSite { get; set; } public int Priority { get; set; } } /// /// Cookie转换帮助类 /// public static class CookieConvertHelper { /// /// 将CookieItem列表转换为Cookie字符串 /// /// Cookie对象列表 /// Cookie字符串,格式:name1=value1; name2=value2 public static string ToCookieString(List cookieItems) { if (cookieItems == null || cookieItems.Count == 0) return string.Empty; var builder = new StringBuilder(); for (int i = 0; i < cookieItems.Count; i++) { var item = cookieItems[i]; if (!string.IsNullOrEmpty(item.Name) && item.Value != null) { builder.Append($"{item.Name}={item.Value}"); if (i < cookieItems.Count - 1) builder.Append("; "); } } return builder.ToString(); } /// /// 将单个CookieItem转换为Cookie字符串 /// /// Cookie对象 /// Cookie字符串,格式:name=value public static string ToCookieString(CookieItem cookieItem) { if (cookieItem == null || string.IsNullOrEmpty(cookieItem.Name)) return string.Empty; return $"{cookieItem.Name}={cookieItem.Value}"; } /// /// 将CookieItem列表转换为字典 /// /// Cookie对象列表 /// Cookie字典 public static Dictionary ToDictionary(List cookieItems) { var dict = new Dictionary(); if (cookieItems == null || cookieItems.Count == 0) return dict; foreach (var item in cookieItems) { if (!string.IsNullOrEmpty(item.Name) && !dict.ContainsKey(item.Name)) { dict.Add(item.Name, item.Value); } } return dict; } /// /// 将CookieItem列表转换为System.Net.CookieCollection /// /// Cookie对象列表 /// 域名 /// CookieCollection public static System.Net.CookieCollection ToCookieCollection(List cookieItems, string domain = "mobile.yangkeduo.com") { var cookieCollection = new System.Net.CookieCollection(); if (cookieItems == null || cookieItems.Count == 0) return cookieCollection; foreach (var item in cookieItems) { if (string.IsNullOrEmpty(item.Name)) continue; var cookie = new System.Net.Cookie { Name = item.Name, Value = item.Value ?? string.Empty, Domain = string.IsNullOrEmpty(item.Domain) ? domain : item.Domain, Path = string.IsNullOrEmpty(item.Path) ? "/" : item.Path, Secure = item.Secure, HttpOnly = item.HttpOnly, Expires = item.Expires ?? DateTime.MinValue }; cookieCollection.Add(cookie); } return cookieCollection; } /// /// 从JSON数组字符串转换为Cookie字符串 /// /// JSON数组字符串 /// Cookie字符串 public static string FromJsonToCookieString(string cookie) { try { var cookieItems = Newtonsoft.Json.JsonConvert.DeserializeObject>(cookie); return ToCookieString(cookieItems); } catch { return string.Empty; } } /// /// 从JSON数组字符串转换为字典 /// /// JSON数组字符串 /// Cookie字典 public static Dictionary FromJsonToDictionary(string jsonArray) { try { var cookieItems = Newtonsoft.Json.JsonConvert.DeserializeObject>(jsonArray); return ToDictionary(cookieItems); } catch { return new Dictionary(); } } }