pan
2025-11-06 42429ae61571ff9563c9e987cd52be2132e77775
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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(  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("AldMaterialWarehousing/MaterialWarehousing", content);
            string body = await response.Content.ReadAsStringAsync();
 
            if (!response.IsSuccessStatusCode)
            {
 
                throw new HttpRequestException(body);
            }
 
            // JsonConvert.DeserializeObject<ResponseModel>(body);
        }
 
        public async Task FeedbackOutbound(  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("AldMaterialOutbound/MaterialOutbound", 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;
            }
        }
 
    }
 
}