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.

152 lines
5.0 KiB
C#

using Newtonsoft.Json;
using SlnMesnac.RfidUpload.Common;
using SlnMesnac.RfidUpload.Model;
using SlnMesnac.RfidUpload.Model.config;
using SlnMesnac.RfidUpload.NLog;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NewLife.Http;
using TouchSocket.Core;
using TouchSocket.Rpc;
using TouchSocket.Sockets;
using TouchSocket.WebApi;
using System.Security.Policy;
namespace SlnMesnac.RfidUpload.TouchSocket
{
public sealed class WebApiClientApp
{
#region 单例实现
private static readonly Lazy<WebApiClientApp> _lazy = new Lazy<WebApiClientApp>(() => new WebApiClientApp());
public static WebApiClientApp Instance
{
get
{
return _lazy.Value;
}
}
#endregion
private LogHelper logger = LogHelper.Instance;
private JsonChange jsonChange = JsonChange.Instance;
private AppConfig appConfig = AppConfig.Instance;
public string UploadAsync(int code,string paramStr,string reqName)
{
string guid = SnowflakeFactory.NewId.ToString();
string result = string.Empty;
try
{
var param = new ContainerInbound()
{
code = code,
requestURL = appConfig.requestURL,
sysCode = appConfig.sysCode,
password = appConfig.password,
ak = appConfig.ak,
sk = appConfig.sk,
containerRegisterQuery = paramStr
};
var str = jsonChange.ModeToJson(param);
logger.Info($"==========={reqName}===========");
logger.Info($"Http请求的:[{guid}] => \r\n {str}");
logger.Info($"Http请求ParamStr的:[{guid}] => \r\n {paramStr}");
result = client.PostJson(appConfig.localUrl, param);
logger.Info($"Http返回的:[{guid}] => \r\n {result}");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = e.Message;
logger.Info($"[{reqName}]请求CSB接口调用服务异常:{e.Message}");
}
return result;
}
public string UploadAsync(string apiName, string paramName, string paramStr,string reqName)
{
string result = string.Empty;
try
{
string guid = SnowflakeFactory.NewId.ToString();
//logger.Info($"请求CSB接口调用服务,参数:{paramStr}");
ContainerInbound container = new ContainerInbound
{
code = 103,
requestURL = appConfig.requestURL,
sysCode = appConfig.sysCode,
password = appConfig.password,
ak = appConfig.ak,
sk = appConfig.sk,
apiName = apiName,
paramName = paramName,
containerRegisterQuery = paramStr
};
var str = jsonChange.ModeToJson(container);
logger.Info($"==========={reqName}===========");
logger.Info($"Http请求的:[{reqName}]:[{guid}] => \r\n {str}");
logger.Info($"Http请求ParamStr的:[{reqName}]:[{guid}] => \r\n {paramStr}");
result = client.PostJson(appConfig.localUrl, container);
logger.Info($"Http返回的:[{reqName}]:[{guid}] => \r\n {result}");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = e.Message;
logger.Info($"[{reqName}]请求CSB接口调用服务异常:{e.Message}");
}
return result;
}
/// <summary>
/// ok
/// </summary>
/// <param name="paramStr"></param>
/// <param name="reqName"></param>
/// <returns></returns>
public string UploadAsync(string paramStr, string reqName)
{
string guid = SnowflakeFactory.NewId.ToString();
string result = string.Empty;
try
{
logger.Info($"==========={reqName}===========");
logger.Info($"Http请求的:[{reqName}]:[{guid}] => \r\n {paramStr}");
result = client.PostJson(appConfig.localUrl, paramStr);
logger.Info($"Http返回的:[{reqName}]:[{guid}] => \r\n{result}");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = e.Message;
logger.Info($"[{reqName}]请求CSB接口调用服务异常:{e.Message}");
}
return result;
}
private HttpClient client = new HttpClient()
{
Timeout = TimeSpan.FromSeconds(30) // 请求超时
};
}
}