!提交代码
This commit is contained in:
182
WCS.BLL/Tool/Api/ApiHelp.cs
Normal file
182
WCS.BLL/Tool/Api/ApiHelp.cs
Normal file
@ -0,0 +1,182 @@
|
||||
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<object> uri, object? query = null, object? body = null)
|
||||
{
|
||||
return Send(HttpMethod.Get, uri, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult<T> Get<T>(string uri, object? query = null, object? body = null)
|
||||
{
|
||||
return Send<T>(HttpMethod.Get, uri, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult<T> Get<T>(IEnumerable<object> uri, object? query = null, object? body = null)
|
||||
{
|
||||
return Send<T>(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<object> uri, object? body = null, object? query = null)
|
||||
{
|
||||
return Send(HttpMethod.Post, uri, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult<T> Post<T>(string uri, object? body = null, object? query = null)
|
||||
{
|
||||
return Send<T>(HttpMethod.Post, uri, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult<T> Post<T>(IEnumerable<object> uri, object? body = null, object? query = null)
|
||||
{
|
||||
return Send<T>(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<object> 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<ApiResult>(con) ?? new ApiResult();
|
||||
}
|
||||
|
||||
public static ApiResult<T> Send<T>(HttpMethod method, string uri, object? query = null, object? body = null)
|
||||
{
|
||||
return Send<T>(method, new string[] { uri }, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult<T> Send<T>(HttpMethod method, IEnumerable<object> 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<T>() { Code = ((int)re.StatusCode).ToString() };
|
||||
|
||||
var con = re.Content.ReadAsStringAsync().Result;
|
||||
return JsonConvert.DeserializeObject<ApiResult<T>>(con) ?? new ApiResult<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 追加路径片段(有更高要求可以使用Flurl库)
|
||||
/// </summary>
|
||||
/// <param name="url">地址,如 https://www.baidu.com</param>
|
||||
/// <param name="segments">路径片段</param>
|
||||
/// <returns>地址</returns>
|
||||
public static string AppendPathSegments(this string url, IEnumerable<object> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置Query参数(有更高要求可以使用Flurl库)
|
||||
/// </summary>
|
||||
/// <param name="url">地址,如 https://www.baidu.com/s</param>
|
||||
/// <param name="values">参数,支持字典和对象</param>
|
||||
/// <returns>地址</returns>
|
||||
public static string SetQueryParams(this string url, object? values)
|
||||
{
|
||||
string urlStr = url;
|
||||
if (values == null)
|
||||
return urlStr;
|
||||
|
||||
List<string> kv = new List<string>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user