ServerConn.cs
6.2 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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace DeviceLibrary
{
public class ServerConn
{
static string inputCounterDataByXRayMachine_URL;
static string DetermineReelStorageLocation_URL;
static ServerConn()
{
inputCounterDataByXRayMachine_URL = ConfigAppSettings.GetValue("inputCounterDataByXRayMachine");
DetermineReelStorageLocation_URL = ConfigAppSettings.GetValue("DetermineReelStorageLocation");
}
public static CountResult inputCounterDataByXRayMachine(string TwoDBarcode, int qty)
{
var wc = new MyWebClient(15000);
//if (string.IsNullOrEmpty(wc.Headers["Content-Type"]))
// wc.Headers.Add("Content-Type", "application/json;charset=UTF-8");
wc.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8";
wc.Encoding = Encoding.UTF8;
var data = new Wiston_Request();
data.userId = "Q14050052";
data.language = 1;
data.requestTime = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
data.data.Add("TwoDBarcode", TwoDBarcode);
data.data.Add("Qty", qty);
data.data.Add("Counter", ConfigHelper.Config.Get("upload_Counter"));
string json = JsonHelper.SerializeObject(data);
string result = "";
int retry = 0;
retry:
try
{
result = wc.UploadString(ConfigHelper.Config.Get("inputCounterDataByXRayMachine"), "POST", json);
return JsonHelper.DeserializeJsonToObject<CountResult>(result);
}
catch (WebException ex) // 先捕获 HTTP 级异常
{
string respBody = null;
if (ex.Response != null)
{
using (var sr = new StreamReader(ex.Response.GetResponseStream()))
respBody = sr.ReadToEnd();
}
LogUtil.info($"inputCounterDataByXRayMachine url:{ConfigHelper.Config.Get("inputCounterDataByXRayMachine")} retry:{retry}, " +
$"status:{(int?)((HttpWebResponse)ex.Response)?.StatusCode}, " +
$"responseBody:{respBody}, " +
$"exception:{ex}");
retry++;
if (retry < 3) goto retry;
return null;
}
catch (Exception e)
{
LogUtil.info($"inputCounterDataByXRayMachine retry:{retry}, {e.ToString()}");
retry++;
if (retry < 3)
goto retry;
return null;
}
finally
{
LogUtil.info($"inputCounterDataByXRayMachine request:{json} response:{result}");
}
}
public static ReelLocation DetermineReelStorageLocation(string TwoDBarcode)
{
var wc = new MyWebClient(15000);
//if (string.IsNullOrEmpty(wc.Headers["Content-Type"]))
// wc.Headers.Add("Content-Type", "application/json;charset=UTF-8");
wc.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8";
wc.Headers.Add("Accept", "application/json");
wc.Encoding = Encoding.UTF8;
var data = new Wiston_Request();
data.userId = "Q14050052";
data.language = 0;
data.requestTime = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
//data.requestTime = 0;
data.data.Add("str2DBarcode", TwoDBarcode);
data.data.Add("labelPrinter", ConfigHelper.Config.Get("upload_labelPrinter"));
string json = JsonHelper.SerializeObject(data);
string result = "";
int retry = 0;
retry:
try
{
result = wc.UploadString(ConfigHelper.Config.Get("DetermineReelStorageLocation"), "POST", json);
return JsonHelper.DeserializeJsonToObject<ReelLocation>(result);
}
catch (WebException ex) // 先捕获 HTTP 级异常
{
string respBody = null;
if (ex.Response != null)
{
using (var sr = new StreamReader(ex.Response.GetResponseStream()))
respBody = sr.ReadToEnd();
}
LogUtil.info($"DetermineReelStorageLocation url:{ConfigHelper.Config.Get("DetermineReelStorageLocation")} retry:{retry}, " +
$"status:{(int?)((HttpWebResponse)ex.Response)?.StatusCode}, " +
$"responseBody:{respBody}, " +
$"exception:{ex}");
retry++;
if (retry < 3) goto retry;
return null;
}
catch (Exception e)
{
LogUtil.info($"DetermineReelStorageLocation retry:{retry}, {e.ToString()}");
retry++;
if (retry < 3)
goto retry;
return null;
}
finally
{
LogUtil.info($"DetermineReelStorageLocation request:{json} response:{result}");
}
}
}
class Wiston_Request
{
public string userId = "";
public int language = 0;
public string requestTime = "";
public Dictionary<string, object> data = new Dictionary<string, object>();
}
class Wiston_Request2
{
public string userId = "";
public int language = 0;
public string requestTime = "";
public Dictionary<string, object> data = new Dictionary<string, object>();
}
public class CountResult
{
public bool result;
public string message;
public string type;
public long responseTime;
public object data;
}
public class ReelLocation {
public string isVacuum;
public string isTower;
public string isVTTower;
public string message;
}
}