WebService.cs
11.0 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace MES
{
public class WebService
{
private Dictionary<string, string> printKey;
private string vender;
private string _ip;
private readonly string WEB_ADDRESS;
public WebService(string ip)
{
_ip = ip;
WEB_ADDRESS = "http://" + ip + "/NariWebService/MESReturnInfo.asmx";
printKey = new Dictionary<string, string>();
}
//public string IP { get; } = "10.144.9.73";
public string Log { private set; get; }
public string ErrInfo { private set; get; }
/// <summary>
/// Ping服务器
/// </summary>
/// <returns></returns>
public bool Ping()
{
try
{
using (System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping())
{
System.Net.NetworkInformation.PingReply result = ping.Send(_ip, 2000);
bool rtn = result.Status == System.Net.NetworkInformation.IPStatus.Success;
if (rtn)
{
ErrInfo = "OK";
Log = "MES Ping OK";
}
else
{
ErrInfo = "MES连接超时";
Log = "MES Timeout, IP=" + _ip;
}
return rtn;
}
}
catch (Exception ex)
{
ErrInfo = ex.Message;
return false;
}
}
/// <summary>
/// 上传供应商编号
/// </summary>
/// <param name="s"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool UploadVender(string s, out string[][] value)
{
value = null;
vender = s;
string json = "{\"VENDER\":\"" + s + "\"}";
string port = "ReturnPurchaseOrderAndMaterial";
try
{
bool rtn = Post(port, json, out string jsonStr);
Log += "\r\n" + port + "\r\nSend: " + json + "\r\nReturn: " + jsonStr;
if (!rtn) return false;
if (jsonStr == "" || jsonStr == "[]" || jsonStr.Contains("Error") || !jsonStr.StartsWith("[{"))
return false;
Newtonsoft.Json.Linq.JArray jArray = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(jsonStr);
value = new string[jArray.Count][];
for (int i = 0; i < value.Length; i++)
{
value[i] = new string[jArray[i].Count()];
value[i][0] = jArray[i]["PURCHASEORDERNO"].ToString();
value[i][1] = jArray[i]["PURCHASEORDERNUMBER"].ToString();
}
ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
Log = ex.Message;
return false;
}
}
/// <summary>
/// 绑定供应商编号和物料编号
/// </summary>
/// <param name="vender"></param>
/// <param name="material"></param>
/// <returns></returns>
public bool UploadMaterial(string vender, string material)
{
string json = "{\"VENDER\":\"" + vender + "\",\"ITEMCODE\":\"" + material + "\"}";
string port = "ReturnPurchaseOrder";
this.vender = vender;
try
{
bool rtn = Post(port, json, out string jsonStr);
Log += "\r\n" + port + "\r\nSend: " + json + "\r\nReturn: " + jsonStr;
if (!rtn) return false;
if (jsonStr == "" || jsonStr == "[]" || jsonStr.Contains("Error") || !jsonStr.StartsWith("[{"))
return false;
ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
Log = ex.Message;
return false;
}
}
/// <summary>
/// 上传订单号,行号
/// </summary>
/// <param name="order"></param>
/// <param name="num"></param>
/// <param name="qty"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool UploadOrder(string order, string num, string qty, out Dictionary<string, string> value)
{
value = new Dictionary<string, string>();
string json = "{\"PURCHASEORDERNO\":\"" + order + "\",\"PURCHASEORDERNUMBER\":\"" + num + "\"}";
string port = "ReturnPurchaseOrderByOrderCodeAndNumber";
try
{
bool rtn = Post(port, json, out string jsonStr);
Log += "\r\n" + port + "\r\nSend: " + json + "\r\nReturn: " + jsonStr;
if (!rtn) return false;
if (jsonStr == "" || jsonStr == "[]" || jsonStr.Contains("Error") || !jsonStr.StartsWith("[{"))
return false;
Newtonsoft.Json.Linq.JArray jArray = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(jsonStr);
//text = new Dictionary<string, string>();
value.Add("MATERIALDEC", jArray[0]["MATERIALDEC"].ToString());
value.Add("STORAGE", jArray[0]["STORAGE"].ToString());
value.Add("MATERIALNO", jArray[0]["MATERIALNO"].ToString());
value.Add("PURCHASEORDERNO", jArray[0]["PURCHASEORDERNO"].ToString());
value.Add("PURCHASEORDERNUMBER", jArray[0]["PURCHASEORDERNUMBER"].ToString());
value.Add("QTY", qty);
//int n = ++number;
//value.Add("BATCHNO", string.Format("{0:yyMMddHHmm}{1:000}", DateTime.Now, n));
value.Add("BATCHNO", string.Format("{0:yyMMddHHmm}", DateTime.Now));
ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
Log = ex.Message;
return false;
}
}
/// <summary>
/// 上传最终数据
/// </summary>
/// <param name="spliceString">二维码字符串</param>
/// <param name="value"></param>
/// <returns></returns>
public bool UploadMessage(string spliceString, out string[] value)
{
value = null;
string json = "{\"PURCHASEORDERNO\":\"" + printKey["PURCHASEORDERNO"] + "\",\"PURCHASEORDERNUMBER\":\"" + printKey["PURCHASEORDERNUMBER"] + "\",\"MATERIALNO\":\"" + printKey["MATERIALNO"] + "\",\"MATERIALLOTNO\":\"" + spliceString + "\",\"VENDERMCODE\":\"" + vender + "\"}";
string port = "InsertPurchaseOrderstringMessage";
try
{
bool rtn = Post(port, json, out string jsonStr);
Log += "\r\n" + port + "\r\nSend: " + json + "\r\nReturn: " + jsonStr;
if (!rtn) return false;
if (jsonStr == "" || jsonStr == "[]" || jsonStr.Contains("Error"))
return false;
Newtonsoft.Json.Linq.JObject jObj = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(jsonStr);
value = new string[2];
value[0] = jObj["Result"].ToString();
value[1] = jObj["Msg"].ToString();
ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
Log = ex.Message;
return false;
}
}
private bool Post(string fun, string json, out string text)
{
text = "";
try
{
string param = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
param += "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">";
param += "<soap12:Body>";
param += "<" + fun + " xmlns=\"http://tempuri.org/\">";
param += "<json>" + json + "</json>";
param += "</" + fun + ">";
param += "</soap12:Body>";
param += "</soap12:Envelope>";
//string param = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
//param += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
//param += "<soap:Body>";
//param += "<" + fun + " xmlns=\"http://tempuri.org/\">";
//param += "<json>" + json + "</json>";
//param += "</" + fun + ">";
//param += "</soap:Body>";
//param += "</soap:Envelope>";
string url = WEB_ADDRESS + "?op=" + fun;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/soap+xml;charset=utf-8";
//request.ContentType = "text/xml;charset=utf-8";
request.Method = "POST";
request.Timeout = 5000;
byte[] buff = Encoding.UTF8.GetBytes(param);
request.ContentLength = buff.Length;
System.IO.Stream writer = request.GetRequestStream();
writer.Write(buff, 0, buff.Length);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.Stream stream = response.GetResponseStream();
System.IO.StreamReader read = new System.IO.StreamReader(stream, Encoding.UTF8);
text = read.ReadToEnd();
read.Close();
//string[] sss = new string[3];
//sss[0] = param;
//sss[1] = url;
//sss[2] = text;
//System.IO.File.WriteAllLines("D:\\111.txt", sss);
string s1 = "<" + fun + "Result>";
string s2 = "</" + fun + "Result>";
int n1 = text.IndexOf(s1);
int n2 = text.IndexOf(s2);
if (n1 == -1 || n2 == -1)
{
Log = "返回的json格式不正确";
ErrInfo = "JSON: " + text;
return false;
}
text = text.Substring(n1 + s1.Length, n2 - n1 - s1.Length);
Log = "Post OK";
ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
Log = ex.Message;
return false;
}
}
}
}