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.

96 lines
3.2 KiB
C#

2 months ago
using Newtonsoft.Json;
using SlnMesnac.RfidUpload.Common;
using SlnMesnac.RfidUpload.Model;
using SlnMesnac.RfidUpload.Model.config;
using SlnMesnac.RfidUpload.NLog;
using System;
2 months ago
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Rpc;
using TouchSocket.Sockets;
using TouchSocket.WebApi;
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
2 months ago
private LogHelper logger = LogHelper.Instance;
private JsonChange jsonChange = JsonChange.Instance;
private AppConfig appConfig = AppConfig.Instance;
private WebApiClientApp() { }
2 months ago
private WebApiClientSlim CreateWebApiClient(string url)
{
2 months ago
var client = new WebApiClientSlim(new System.Net.Http.HttpClient());
client.Setup(new TouchSocketConfig()
.SetRemoteIPHost("http://127.0.0.1:9090")
.ConfigurePlugins(a =>
{
}));
return client;
}
2 months ago
public string UploadAsync(int code,string paramStr)
{
2 months ago
string result = string.Empty;
try
{
var param = new ContainerInbound()
{
2 months ago
code = code,
requestURL = appConfig.requestURL,
sysCode = appConfig.sysCode,
password = appConfig.password,
ak = appConfig.ak,
sk = appConfig.sk,
containerRegisterQuery = paramStr
};
2 months ago
var str = jsonChange.ModeToJson(param);
logger.Info($"请求CSB接口调用服务,参数:{str}");
result = Post("http://127.0.0.1:9090/api/uploadCsb", param);
logger.Info($"CSB接口返回信息:{result}");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
2 months ago
result = e.Message;
logger.Info($"请求CSB接口调用服务异常:{e.Message}");
}
2 months ago
return result;
}
public static string Post(string url, object postData)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/json";
req.Timeout = 5000;
if (req == null) return string.Empty;
2 months ago
byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(postData));
using (Stream reqStream = req.GetRequestStream())
reqStream.Write(data, 0, data.Length);
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
using (Stream stream = resp.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
return reader.ReadToEnd();
}
}
}