using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using WCS.BLL.Tool.Api.Models; namespace WCS.BLL.Tool.Api { public static class ApiHelp { public static HttpClient httpClient; static ApiHelp() { httpClient = new HttpClient(); httpClient.Timeout = TimeSpan.FromSeconds(5); httpClient.DefaultRequestHeaders.Add("User-Agent", "Chrome/95.0.4638.69 Safari/537.36"); //ServicePointManager.Expect100Continue = false; } public static ApiResult Get(string uri, object? query = null, object? body = null) { return Send(HttpMethod.Get, uri, query, body); } public static ApiResult Get(IEnumerable uri, object? query = null, object? body = null) { return Send(HttpMethod.Get, uri, query, body); } public static ApiResult Get(string uri, object? query = null, object? body = null) { return Send(HttpMethod.Get, uri, query, body); } public static ApiResult Get(IEnumerable uri, object? query = null, object? body = null) { return Send(HttpMethod.Get, uri, query, body); } public static ApiResult Post(string uri, object? body = null, object? query = null) { return Send(HttpMethod.Post, uri, query, body); } public static ApiResult Post(IEnumerable uri, object? body = null, object? query = null) { return Send(HttpMethod.Post, uri, query, body); } public static ApiResult Post(string uri, object? body = null, object? query = null) { return Send(HttpMethod.Post, uri, query, body); } public static ApiResult Post(IEnumerable uri, object? body = null, object? query = null) { return Send(HttpMethod.Post, uri, query, body); } public static ApiResult Send(HttpMethod method, string uri, object? query = null, object? body = null) { return Send(method, new string[] { uri }, query, body); } public static ApiResult Send(HttpMethod method, IEnumerable uri, object? query = null, object? body = null) { HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, "".AppendPathSegments(uri).SetQueryParams(query)); if (body != null) { var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); httpRequestMessage.Content = content; } var re = httpClient.SendAsync(httpRequestMessage).Result; if (!re.IsSuccessStatusCode) return new ApiResult() { Code = ((int)re.StatusCode).ToString() }; var con = re.Content.ReadAsStringAsync().Result; return JsonConvert.DeserializeObject(con) ?? new ApiResult(); } public static ApiResult Send(HttpMethod method, string uri, object? query = null, object? body = null) { return Send(method, new string[] { uri }, query, body); } public static ApiResult Send(HttpMethod method, IEnumerable uri, object? query = null, object? body = null) { HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, "".AppendPathSegments(uri).SetQueryParams(query)); if (body != null) { var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); httpRequestMessage.Content = content; } var re = httpClient.SendAsync(httpRequestMessage).Result; if (!re.IsSuccessStatusCode) return new ApiResult() { Code = ((int)re.StatusCode).ToString() }; var con = re.Content.ReadAsStringAsync().Result; return JsonConvert.DeserializeObject>(con) ?? new ApiResult(); } /// /// 追加路径片段(有更高要求可以使用Flurl库) /// /// 地址,如 https://www.baidu.com /// 路径片段 /// 地址 public static string AppendPathSegments(this string url, IEnumerable segments) { string urlStr = url; foreach (var segment in segments) { var val = segment?.ToString(); if (string.IsNullOrWhiteSpace(val)) continue; if (urlStr.EndsWith("/")) urlStr = urlStr.Substring(0, urlStr.Length - 1); urlStr += val!.StartsWith("/") ? val : $"/{val}"; } return urlStr; } /// /// 设置Query参数(有更高要求可以使用Flurl库) /// /// 地址,如 https://www.baidu.com/s /// 参数,支持字典和对象 /// 地址 public static string SetQueryParams(this string url, object? values) { string urlStr = url; if (values == null) return urlStr; List kv = new List(); if (values is IEnumerable jh) { if (jh is IDictionary dict) { foreach (DictionaryEntry item in dict) kv.Add($"{item.Key?.ToString()}={item.Value?.ToString()}"); } } else { foreach (var item in values.GetType().GetProperties()) { if (item.CanRead) kv.Add($"{item.Name}={item.GetValue(values)?.ToString()}"); } } if (kv.Any()) { if (!urlStr.Contains("?")) urlStr += "?"; else { if (!urlStr.EndsWith("&")) urlStr += "&"; } urlStr += string.Join("&", kv); } return urlStr; } } }