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.

61 lines
2.3 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Extensions;
using Newtonsoft.Json;
using NLog;
namespace DS.Module.HkOpenApi;
public class HttpRequestService:IHttpRequestService
{
private readonly IServiceProvider _serviceProvider;
// private readonly ISqlSugarClient db;
// private readonly IUser user;
private readonly string ip;
private readonly int port;
private readonly string accessKey;
private readonly string accessSecret;
static readonly Logger Logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// 构造函数
/// </summary>
/// <param name="serviceProvider"></param>
public HttpRequestService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
// db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
// user = _serviceProvider.GetRequiredService<IUser>();
ip = AppSetting.app(new string[] { "HKOpenApi", "IP" }).ObjToString();
port = AppSetting.app(new string[] { "HKOpenApi", "Port" }).ToInt();
accessKey = AppSetting.app(new string[] { "HKOpenApi", "AccessKey" }).ObjToString();
accessSecret = AppSetting.app(new string[] { "HKOpenApi", "AccessSecret" }).ObjToString();
// 设置平台信息参数合作方APPKey、合作方APPSecret、平台IP、平台端口、以及是否适用HTTPS协议
// 只要平台信息参数一致,多个请求只需设置一次参数
// HttpUtillib.SetPlatformInfo(accessKey, accessSecret, ip, port, true);
}
public HKDataResult HKHttpPost(HKPostData model)
{
// 只要平台信息参数一致,多个请求只需设置一次参数
HttpUtillib.SetPlatformInfo(accessKey, accessSecret, ip, port, true);
HKDataResult res = new HKDataResult();
// 发起POST请求超时时间15秒返回响应字节数组
string result = HttpUtillib.HttpPost(model.Url, model.Data, 15);
if (null == result)
{
// 请求失败
// Console.WriteLine("/artemis/api/resource/v1/cameras/indexCode: POST fail");
}
else
{
res = JsonConvert.DeserializeObject<HKDataResult>(result);
// Console.WriteLine(System.Text.Encoding.UTF8.GetString(result));
}
return res;
}
}