You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
5.5 KiB
C#
173 lines
5.5 KiB
C#
using Estsh.Client.Common.Util;
|
|
using Estsh.Core.Model.Result;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Estsh.Client.Common.HttpClientUtil
|
|
{
|
|
public class HttpClientHelper
|
|
{
|
|
/// <summary>
|
|
/// 服务
|
|
/// </summary>
|
|
private static IServiceProvider serviceProvider = null;
|
|
private static HttpClient client = null;
|
|
private readonly static string encryptKey = "estsh.mes.selectupdate";
|
|
|
|
/// <summary>
|
|
/// 初始化服务,没给服务对象就自己创建
|
|
/// </summary>
|
|
/// <param name="provider">如果是asp.net可以直接抛过来ConfigureServices的services</param>
|
|
public static void InitService(IServiceProvider provider,string httpClientName)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(provider, nameof(provider));
|
|
serviceProvider = provider;
|
|
client = GetHttpClientFactory().CreateClient(httpClientName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 得到IHttpClientFactory的对象
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static IHttpClientFactory GetHttpClientFactory()
|
|
{
|
|
return serviceProvider.GetService<IHttpClientFactory>();
|
|
|
|
}
|
|
|
|
public static HttpClient GetHttpClient()
|
|
{
|
|
return client;
|
|
|
|
}
|
|
|
|
public int Execute(string cmd, Dictionary<string, object>? Params = null)
|
|
{
|
|
RequestBody requestBody = new RequestBody();
|
|
requestBody.SqlString = SecurityEncDecrypt.EncryptDES(cmd, encryptKey);
|
|
if (Params == null)
|
|
{
|
|
requestBody.Params = new Dictionary<string, object>();
|
|
}
|
|
else
|
|
{
|
|
requestBody.Params = Params;
|
|
}
|
|
string retust = Post("DataHandle/Execute", requestBody);
|
|
return JsonConvert.DeserializeObject<int>(retust);
|
|
}
|
|
|
|
|
|
public List<dynamic> GetEntityList(string cmd, Dictionary<string, object>? Params = null)
|
|
{
|
|
RequestBody requestBody = new RequestBody();
|
|
requestBody.SqlString = SecurityEncDecrypt.EncryptDES(cmd, encryptKey);
|
|
if (Params == null)
|
|
{
|
|
requestBody.Params = new Dictionary<string, object>();
|
|
}
|
|
else
|
|
{
|
|
requestBody.Params = Params;
|
|
}
|
|
string retust = Post("DataHandle/GetEntityList", requestBody);
|
|
return JsonConvert.DeserializeObject<List<dynamic>>(retust) ;
|
|
}
|
|
|
|
|
|
public object GetScalar(string cmd, Dictionary<string, object>? Params = null)
|
|
{
|
|
RequestBody requestBody = new RequestBody();
|
|
requestBody.SqlString = SecurityEncDecrypt.EncryptDES(cmd, encryptKey);
|
|
if (Params == null)
|
|
{
|
|
requestBody.Params = new Dictionary<string, object>();
|
|
}
|
|
else
|
|
{
|
|
requestBody.Params = Params;
|
|
}
|
|
string retust = Post("DataHandle/GetScalar", requestBody);
|
|
return JsonConvert.DeserializeObject<object>(retust);
|
|
}
|
|
|
|
|
|
|
|
// <summary>
|
|
// Post请求数据--同步方法
|
|
// </summary>
|
|
// <param name="actionUri">例如/api/Files/UploadFile</param>
|
|
// <param name="body">请求参数</param>
|
|
// <returns></returns>
|
|
public string Post(string actionUri, RequestBody body)
|
|
{
|
|
try
|
|
{
|
|
string requestUri = client.BaseAddress + actionUri;
|
|
|
|
var content = new StringContent(JsonConvert.SerializeObject(body),Encoding.UTF8, "application/json");
|
|
|
|
var result = client.PostAsync(requestUri, content).GetAwaiter().GetResult();
|
|
|
|
if (result.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
string str = result.Content.ReadAsStringAsync().Result;
|
|
return str;
|
|
}
|
|
else
|
|
{
|
|
return string.Empty;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Error("调用API异常", ex);
|
|
throw ex;
|
|
}
|
|
|
|
}
|
|
|
|
// <summary>
|
|
// Post请求数据-异步方法
|
|
// </summary>
|
|
// <param name="actionUri">例如/api/Files/UploadFile</param>
|
|
// <param name="body">请求参数</param>
|
|
// <returns></returns>
|
|
public async Task<string> PostAsync(string actionUri, RequestBody body)
|
|
{
|
|
try
|
|
{
|
|
string requestUri = client.BaseAddress + actionUri;
|
|
|
|
var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
|
|
|
|
var result = await client.PostAsync(requestUri, content);
|
|
|
|
if (result.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
string str = result.Content.ReadAsStringAsync().Result;
|
|
return str;
|
|
}
|
|
else
|
|
{
|
|
return string.Empty;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Error("调用API异常", ex);
|
|
throw ex;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|