using Microsoft.Extensions.Logging;
|
using Newtonsoft.Json;
|
using Org.BouncyCastle.Asn1.Ocsp;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net;
|
using System.Net.Http;
|
using System.Security.Policy;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEA_DTO.Basic;
|
using WIDESEA_DTO.Inbound;
|
using WIDESEA_DTO.Outbound;
|
using WIDESEA_IBasicService;
|
|
namespace WIDESEA_BasicService
|
{
|
public class InvokeMESService : IInvokeMESService
|
{
|
private readonly IHttpClientFactory _httpClientFactory;
|
private readonly ILogger<InvokeMESService> _logger;
|
private string UserName = "12312";
|
private string Password = "1";
|
public InvokeMESService(IHttpClientFactory httpClientFactory, ILogger<InvokeMESService> logger)
|
{
|
_httpClientFactory = httpClientFactory;
|
_logger = logger;
|
}
|
|
public async Task FeedbackInbound(string url, FeedbackInboundRequestModel model)
|
{
|
string json = JsonConvert.SerializeObject(model);
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
var _client = _httpClientFactory.CreateClient("MESUrl");
|
_client.DefaultRequestHeaders.Clear();
|
_client.DefaultRequestHeaders.Add("Accept", "application/json");
|
|
var response = await _client.PostAsync(url, content);
|
string body = await response.Content.ReadAsStringAsync();
|
|
if (!response.IsSuccessStatusCode)
|
{
|
|
throw new HttpRequestException(body);
|
}
|
|
// JsonConvert.DeserializeObject<ResponseModel>(body);
|
}
|
|
public async Task FeedbackOutbound(string url, FeedbackOutboundRequestModel model)
|
{
|
string json = JsonConvert.SerializeObject(model);
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
var _client = _httpClientFactory.CreateClient("MESUrl");
|
_client.DefaultRequestHeaders.Clear();
|
_client.DefaultRequestHeaders.Add("Accept", "application/json");
|
|
var response = await _client.PostAsync(url, content);
|
string body = await response.Content.ReadAsStringAsync();
|
|
if (!response.IsSuccessStatusCode)
|
{
|
|
throw new HttpRequestException(body);
|
}
|
|
// JsonConvert.DeserializeObject<ResponseModel>(body);
|
}
|
|
public async Task<ResponseModel> NewMaterielToMes(MaterielToMesDTO model)
|
{
|
string json = JsonConvert.SerializeObject(model);
|
|
|
string userDataEncoded = Uri.EscapeDataString(json);
|
//string baseUrl = "http://mestest.ald.com//OrBitWCFServiceR15/orbitwebapi.ashx?";
|
string userTicket = await GetToken(UserName, Password);
|
string api = "WMS_BarcodeInformation";
|
|
|
var client = _httpClientFactory.CreateClient("MESUrl");
|
// 拼接 URL 参数
|
string url = $"{client.BaseAddress}UserTicket={userTicket}&API={api}&UserData={userDataEncoded}";
|
|
|
client.DefaultRequestHeaders.Clear();
|
|
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
|
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
using var response = await client.PostAsync(url, content);
|
var responseText = await response.Content.ReadAsStringAsync();
|
if (!response.IsSuccessStatusCode)
|
{
|
throw new HttpRequestException(responseText);
|
}
|
|
return JsonConvert.DeserializeObject<ResponseModel>(responseText);
|
}
|
|
public async Task<string> GetToken(String username, string password)
|
{
|
|
var clientHandler = new HttpClientHandler
|
{
|
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
|
};
|
//var client = new HttpClient(clientHandler);
|
var client = _httpClientFactory.CreateClient("MESUrl");
|
client.DefaultRequestHeaders.Clear();
|
|
|
|
var request = new HttpRequestMessage
|
{
|
Method = HttpMethod.Post,
|
RequestUri = new Uri($"{client.BaseAddress}UserName={username}&UserPassword={password}"),
|
Headers ={
|
{ "Accept", "*/*" },
|
{ "User-Agent", "PostmanRuntime-ApipostRuntime/1.1.0" },
|
{ "Connection", "keep-alive" },
|
},
|
Content = new MultipartFormDataContent
|
{
|
},
|
};
|
using (var response = await client.SendAsync(request))
|
{
|
response.EnsureSuccessStatusCode();
|
var body = await response.Content.ReadAsStringAsync();
|
if (!response.IsSuccessStatusCode)
|
{
|
|
throw new HttpRequestException(body);
|
}
|
|
return body;
|
}
|
}
|
|
}
|
|
}
|