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.

145 lines
4.9 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;
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 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($"请求CSB接口调用服务,参数:{str}");
result = Post(appConfig.localUrl, param);
logger.Info($"CSB接口返回信息:{result}");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = e.Message;
logger.Info($"请求CSB接口调用服务异常:{e.Message}");
}
return result;
}
public string UploadAsync(string apiName, string paramName, string paramStr)
{
string result = string.Empty;
try
{
//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($"请求CSB接口调用服务,参数:{str}");
result = Post(appConfig.localUrl, container);
logger.Info($"CSB接口返回信息:{result}");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = e.Message;
logger.Info($"请求CSB接口调用服务异常:{e.Message}");
}
return result;
}
public string UploadAsync(string paramStr)
{
string result = string.Empty;
try
{
logger.Info($"请求CSB接口调用服务,参数:{paramStr}");
result = Post(appConfig.localUrl, paramStr);
logger.Info($"CSB接口返回信息:{result}");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = e.Message;
logger.Info($"请求CSB接口调用服务异常:{e.Message}");
}
return result;
}
private HttpClient client = new HttpClient();
public string Post(string url, object postData)
{
var res = client.PostJson(url,postData);
return res;
//HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
//req.Method = "POST";
//req.ContentType = "application/json";
//req.Timeout = 5000;
//if (req == null) return string.Empty;
//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();
}
}
}