|
|
|
@ -0,0 +1,252 @@
|
|
|
|
|
using Admin.Core.Api.PLTBusiness.Entity;
|
|
|
|
|
using Admin.Core.IService;
|
|
|
|
|
using Admin.Core.Service;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using RabbitMQ.Client;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Security.Policy;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Admin.Core.Api.PLTBusiness
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class HttpService : BackgroundService
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
|
|
|
|
|
|
|
|
|
private string _token;
|
|
|
|
|
|
|
|
|
|
private int _tokenExpirationTime;
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, string> _tokenDictionary = new Dictionary<string, string>()
|
|
|
|
|
{
|
|
|
|
|
{"authCode", "QT_XXX"},
|
|
|
|
|
{"appid", "q7cloud_XXX"},
|
|
|
|
|
{"secret", "f434eab8b9652524dc719e33f4ff255a45570XXX"}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private string _host;
|
|
|
|
|
|
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
|
|
|
|
|
public HttpService(IHttpClientFactory httpClientFactory, IServiceScopeFactory scopeFactory)
|
|
|
|
|
{
|
|
|
|
|
_httpClientFactory = httpClientFactory;
|
|
|
|
|
_scopeFactory = scopeFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async override Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
|
{
|
|
|
|
|
using var scope = _scopeFactory.CreateScope();
|
|
|
|
|
var services = scope.ServiceProvider;
|
|
|
|
|
var xlBusiness = services.GetService<XlBusiness>();
|
|
|
|
|
|
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//执行任务
|
|
|
|
|
Console.WriteLine($"{DateTime.Now}");
|
|
|
|
|
|
|
|
|
|
await xlBusiness.InsertNewMaterialData(null);
|
|
|
|
|
|
|
|
|
|
await Task.Delay(1000*10);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void GetToken()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string body = JsonSerializer.Serialize(_tokenDictionary);
|
|
|
|
|
string tokenJson = await PostAsync(_host + "/qtn/api/auth/token", body, "application/json", "application/json", null, 10);
|
|
|
|
|
ResponseRoot<TokenData> responseRoot = JsonSerializer.Deserialize<ResponseRoot<TokenData>>(tokenJson);
|
|
|
|
|
if (responseRoot == null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Token获取失败!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(responseRoot.code != 200)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("ERROR 状态码:" + responseRoot.code);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_token = responseRoot.Data.token;
|
|
|
|
|
_tokenExpirationTime = responseRoot.Data.expiration;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("ERROR: " + ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发起POST异步请求
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url">请求地址</param>
|
|
|
|
|
/// <param name="body">POST提交的内容</param>
|
|
|
|
|
/// <param name="bodyMediaType">POST内容的媒体类型,如:application/xml、application/json</param>
|
|
|
|
|
/// <param name="responseContentType">HTTP响应上的content-type内容头的值,如:application/xml、application/json、application/text、application/x-www-form-urlencoded等</param>
|
|
|
|
|
/// <param name="headers">请求头信息</param>
|
|
|
|
|
/// <param name="timeOut">请求超时时间,单位秒</param>
|
|
|
|
|
/// <returns>返回string</returns>
|
|
|
|
|
private async Task<string> PostAsync(string url, string body,
|
|
|
|
|
string bodyMediaType = null,
|
|
|
|
|
string responseContentType = null,
|
|
|
|
|
Dictionary<string, string> headers = null,
|
|
|
|
|
int timeOut = 30)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var hostName = GetHostName(url);
|
|
|
|
|
using (HttpClient client = _httpClientFactory.CreateClient(hostName))
|
|
|
|
|
{
|
|
|
|
|
client.Timeout = TimeSpan.FromSeconds(timeOut);
|
|
|
|
|
if (headers?.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (string key in headers.Keys)
|
|
|
|
|
{
|
|
|
|
|
client.DefaultRequestHeaders.Add(key, headers[key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
StringContent content = new StringContent(body, System.Text.Encoding.UTF8, mediaType: bodyMediaType);
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(responseContentType))
|
|
|
|
|
{
|
|
|
|
|
content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(responseContentType);
|
|
|
|
|
}
|
|
|
|
|
using (HttpResponseMessage response = await client.PostAsync(url, content))
|
|
|
|
|
{
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
string responseString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
return responseString;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"ERROR: {ex.ToString()} Time: {DateTime.Now}");
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发起GET异步请求
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">返回类型</typeparam>
|
|
|
|
|
/// <param name="url">请求地址</param>
|
|
|
|
|
/// <param name="headers">请求头信息</param>
|
|
|
|
|
/// <param name="timeOut">请求超时时间,单位秒</param>
|
|
|
|
|
/// <returns>返回string</returns>
|
|
|
|
|
private async Task<string> GetAsync(string url, Dictionary<string, string> headers = null, int timeOut = 30)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var hostName = GetHostName(url);
|
|
|
|
|
using (HttpClient client = _httpClientFactory.CreateClient(hostName))
|
|
|
|
|
{
|
|
|
|
|
client.Timeout = TimeSpan.FromSeconds(timeOut);
|
|
|
|
|
if (headers?.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (string key in headers.Keys)
|
|
|
|
|
{
|
|
|
|
|
client.DefaultRequestHeaders.Add(key, headers[key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
using (HttpResponseMessage response = await client.GetAsync(url))
|
|
|
|
|
{
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
string responseString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
return responseString;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"ERROR: {ex.ToString()} Time: {DateTime.Now}");
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task Post(string url, Dictionary<string, string> headers = null)
|
|
|
|
|
{
|
|
|
|
|
var hostName = GetHostName(url);
|
|
|
|
|
// 创建 HttpClient 实例
|
|
|
|
|
var client = _httpClientFactory.CreateClient(hostName);
|
|
|
|
|
|
|
|
|
|
// 定义要发送的对象
|
|
|
|
|
//var data = new
|
|
|
|
|
//{
|
|
|
|
|
// Name = "John Doe",
|
|
|
|
|
// Age = 30,
|
|
|
|
|
// Email = "john.doe@example.com"
|
|
|
|
|
//};
|
|
|
|
|
|
|
|
|
|
// 序列化对象为 JSON
|
|
|
|
|
var json = JsonSerializer.Serialize(headers);
|
|
|
|
|
|
|
|
|
|
// 创建 HttpContent,设置内容为 JSON 格式
|
|
|
|
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
|
|
|
|
|
|
// 发送 POST 请求
|
|
|
|
|
var response = await client.PostAsync(url, content);
|
|
|
|
|
|
|
|
|
|
// 检查响应状态码
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
// 读取响应内容
|
|
|
|
|
var responseBody = await response.Content.ReadAsStringAsync();
|
|
|
|
|
Console.WriteLine($"Response: {responseBody}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Error: {response.StatusCode}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 私有函数
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取请求的主机名
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static string GetHostName(string url)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(url))
|
|
|
|
|
{
|
|
|
|
|
return url.Replace("https://", "").Replace("http://", "").Split('/')[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return "AnyHost";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|