using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Web; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace BarrageGrab { /// /// 扩展类 /// public static class ObjectExtension { /// /// 判断字符串是否为空或空白 /// public static bool IsNullOrWhiteSpace(this string str) => string.IsNullOrWhiteSpace(str); /// /// 判断字符串是否为空 /// public static bool IsNullOrEmpty(this string str) => string.IsNullOrEmpty(str); /// /// 转为Json /// /// /// public static string ToJson(this object obj,bool format = false) { if (obj is string) { return obj.ToString(); } return JsonConvert.SerializeObject(obj, format?Formatting.Indented:Formatting.None); } /// /// 转为对象 /// /// /// /// public static T AsObject(this string json) { return JsonConvert.DeserializeObject(json); } /// /// 获取Value或其Value默认值 /// public static TValue ValueOrDefault(this IDictionary dic, Tkey key) { if (dic.ContainsKey(key)) { return dic[key]; } return default(TValue); } /// /// 获取Value或其Value默认值 /// public static TValue ValueOrDefault(this IDictionary dic, Tkey key, TValue defaultValue) { if (dic.ContainsKey(key)) { return dic[key]; } return defaultValue; } /// /// 遍历集合 /// public static void Foreach(this IEnumerable list, Action func) { foreach (var item in list) func(item); } /// /// 遍历集合 /// public static void Foreach(this IEnumerable list, Action func) { int i = 0; foreach (var item in list) func(item, i++); } /// /// 是否包含 /// /// /// /// public static bool In(this T obj, params T[] args) { return args.Contains(obj); } /// /// 是否包含 /// /// /// /// public static bool LikeIn(this string str, params string[] args) { return args.Any(a => str.Contains(a)); } public static CookieCollection ToCookies(this string cookieStr, string domain) { var cookies = new CookieCollection(); if (!string.IsNullOrEmpty(cookieStr)) { var cookieArray = cookieStr.Split(';'); foreach (var cookie in cookieArray) { var cookieParts = cookie.Split('='); if (cookieParts.Length == 2) { var cookieName = cookieParts[0].Trim(); var cookieValue = cookieParts[1].Trim(); var newCookie = new Cookie(cookieName, Uri.EscapeDataString(cookieValue)); newCookie.Path = "/"; newCookie.Domain = domain; cookies.Add(newCookie); } } } return cookies; } /// /// 追加另一个字典 /// /// /// /// public static IDictionary Append(this IEnumerable> dic, IEnumerable> other) { var res = new Dictionary(); foreach (var item in dic) { res.Add(item.Key, item.Value); } foreach (var item in other) { if (res.ContainsKey(item.Key)) { res[item.Key] = item.Value; } else { res.Add(item.Key, item.Value); } } return res; } /// /// 转换为Cookie对象列表 /// /// /// /// public static CookieCollection ToCookies(this IDictionary dic, string domain) { var cookieCollection = new CookieCollection(); foreach (var item in dic) { cookieCollection.Add(new Cookie(item.Key, item.Value, "/", domain)); } return cookieCollection; } /// /// 添加Cookie /// /// /// /// public static void PushCookies(this CookieContainer cookies, string domain, IDictionary dic) { if (cookies == null) return; var cookieCollection = dic.ToCookies(domain); cookies.Add(cookieCollection); } /// /// 将字典转为查询参数 /// /// /// public static string ToQueryParam(this IDictionary dic, bool encode = true) { if (dic == null || dic.Count == 0) return string.Empty; var queryParams = dic.Select(kvp => kvp.Key + "=" + (encode ? WebUtility.UrlEncode(kvp.Value) : kvp.Value)); return string.Join("&", queryParams); } /// /// 赋值字典,没有则添加,有则覆盖 /// /// /// /// /// /// public static void Set(this IDictionary dic, TKey key, TValue value) { if (dic.ContainsKey(key)) { dic[key] = value; } else { dic.Add(key, value); } } /// /// 赋值字典,没有则添加,有则覆盖 /// /// /// /// /// public static void Set(this IDictionary dic, KeyValuePair kv) { dic.Set(kv.Key, kv.Value); } /// /// 获取Uri查询参数 /// /// /// /// public static IDictionary GetQueryParams(this UriBuilder uri) { if (uri == null) { throw new ArgumentNullException(nameof(uri)); } var queryParams = uri.Query.TrimStart('?'); if (string.IsNullOrEmpty(queryParams)) { return new Dictionary(); } return queryParams .Split('&') .Select(param => param.Split('=')) .ToDictionary( keyValuePair => Uri.UnescapeDataString(keyValuePair[0]), keyValuePair => Uri.UnescapeDataString(keyValuePair.Length > 1 ? keyValuePair[1] : "")); } /// /// /// /// /// public static void AddQueryParam(this UriBuilder uri, IEnumerable> vals) { if (uri == null) return; var dic = uri.GetQueryParams(); foreach (var item in vals) { dic.Set(item); } uri.Query = dic.ToQueryParam(); } /// /// 删除一个query项 /// /// /// public static void RemoveQueryParam(this UriBuilder uri, string key) { if (uri == null) return; var dic = uri.GetQueryParams(); dic.Remove(key); if (dic.Count > 0) { uri.Query = "?" + dic.ToQueryParam(); } else { uri.Query = ""; } } /// /// 添加参数 /// /// /// /// /// public static void AddQueryParam(this UriBuilder uri, string key, string value) { if (uri == null) return; uri.AddQueryParam(new[] { new KeyValuePair(key,value) }); } } }