InvokeHelper.java
9.75 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
299
300
301
302
package com.huaheng.test;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
public class InvokeHelper {
public static String POST_K3CloudURL = "http://127.0.0.1/K3Cloud/";
private static String CookieVal = null;
private static Map<String, String> map = new HashMap();
static {
map.put("Save", "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc");
map.put("View", "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc");
map.put("Submit", "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc");
map.put("Audit", "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc");
map.put("UnAudit", "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.UnAudit.common.kdsvc");
map.put("StatusConvert", "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.StatusConvert.common.kdsvc");
map.put("ExecuteBillQuery", "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc");
}
private static HttpURLConnection initUrlConn(String url, JSONArray paras) throws Exception {
URL postUrl = new URL(POST_K3CloudURL.concat(url));
HttpURLConnection connection = (HttpURLConnection)postUrl.openConnection();
if (CookieVal != null) {
connection.setRequestProperty("Cookie", CookieVal);
}
if (!connection.getDoOutput()) {
connection.setDoOutput(true);
}
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
UUID uuid = UUID.randomUUID();
int hashCode = uuid.toString().hashCode();
JSONObject jObj = new JSONObject();
jObj.put("format", 1);
jObj.put("useragent", "ApiClient");
jObj.put("rid", hashCode);
jObj.put("parameters", chinaToUnicode(paras.toString()));
jObj.put("timestamp", (new Date()).toString());
jObj.put("v", "1.0");
out.writeBytes(jObj.toString());
out.flush();
out.close();
return connection;
}
public static boolean Login(String dbId, String user, String pwd, int lang) throws Exception {
boolean bResult = false;
String sUrl = "Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc";
JSONArray jParas = new JSONArray();
jParas.put(dbId);
jParas.put(user);
jParas.put(pwd);
jParas.put(lang);
HttpURLConnection connection = initUrlConn(sUrl, jParas);
String key = null;
for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++) {
if (key.equalsIgnoreCase("Set-Cookie")) {
String tempCookieVal = connection.getHeaderField(i);
if (tempCookieVal.startsWith("kdservice-sessionid")) {
CookieVal = tempCookieVal;
break;
}
}
}
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
System.out.println(" ============================= ");
System.out.println(" Contents of post request ");
System.out.println(" ============================= "); String line;
while ((line = reader.readLine()) != null) {
String sResult = new String(line.getBytes(), "utf-8");
System.out.println(sResult);
bResult = line.contains("\"LoginResultType\":1");
}
System.out.println(" ============================= ");
System.out.println(" Contents of post request ends ");
System.out.println(" ============================= ");
reader.close();
connection.disconnect();
return bResult;
}
public static String Save(String formId, String content) throws Exception { return Invoke("Save", formId, content); }
public static String View(String formId, String content) throws Exception { return Invoke("View", formId, content); }
public static void ExecuteBillQuery(String content) throws Exception { Invoke("ExecuteBillQuery", content); }
public static void Submit(String formId, String content) throws Exception { Invoke("Submit", formId, content); }
public static void Audit(String formId, String content) throws Exception { Invoke("Audit", formId, content); }
public static void UnAudit(String formId, String content) throws Exception { Invoke("UnAudit", formId, content); }
public static void StatusConvert(String formId, String content) throws Exception { Invoke("StatusConvert", formId, content); }
private static String Invoke(String deal, String formId, String content) throws Exception {
String sUrl = ((String)map.get(deal)).toString();
JSONArray jParas = new JSONArray();
jParas.put(formId);
jParas.put(content);
HttpURLConnection connectionInvoke = initUrlConn(sUrl, jParas);
BufferedReader reader = new BufferedReader(new InputStreamReader(connectionInvoke.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
String sResult = new String(line.getBytes(), "utf-8");
System.out.println(sResult);
sb.append(sResult);
}
reader.close();
connectionInvoke.disconnect();
return sb.toString();
}
private static void Invoke(String deal, String content) throws Exception {
String sUrl = ((String)map.get(deal)).toString();
JSONArray jParas = new JSONArray();
jParas.put(content);
HttpURLConnection connectionInvoke = initUrlConn(sUrl, jParas);
BufferedReader reader = new BufferedReader(new InputStreamReader(connectionInvoke.getInputStream()));
System.out.println(" ============================= ");
System.out.println(" Contents of post request ");
System.out.println(" ============================= "); String line;
while ((line = reader.readLine()) != null) {
String sResult = new String(line.getBytes(), "utf-8");
System.out.println(sResult);
}
System.out.println(" ============================= ");
System.out.println(" Contents of post request ends ");
System.out.println(" ============================= ");
reader.close();
connectionInvoke.disconnect();
}
public static String chinaToUnicode(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= ' ' && c <= '~') {
sb.append(c);
} else {
sb.append(char2Unicode(c));
}
}
System.out.println("转编码字符======>" + sb);
return sb.toString();
}
private static String char2Unicode(char c) {
StringBuffer sb = new StringBuffer();
sb.append("\\u");
int j = c >>> '\b';
String tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
j = c & 0xFF;
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
return sb.toString();
}
public static Map<String, String> cruxChar2General(Map<String, String> map) {
Set<Map.Entry<String, String>> set = map.entrySet();
for (Map.Entry<String, String> entry : set) {
String str = (String)entry.getValue();
if (str.contains("\"")) {
map.put(entry.getKey(), str.replace("\"", "'")); continue;
} if (str.contains("\\")) {
map.put(entry.getKey(), str.replace("\\", "/"));
}
}
return map;
}
public static String chinaToUnicode2(String str) {
char[] chars = str.toCharArray();
String result = "";
for (int i = 0; i < chars.length; i++) {
int chr1 = chars[i];
if (isChineseByBlock(chars[i])) {
result = result + "\\u" + Integer.toHexString(chr1);
} else if (isChinesePunctuation(chars[i])) {
result = result + ".";
} else {
result = result + chars[i];
}
}
return result;
}
public static boolean isChinesePunctuation(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS || ub == Character.UnicodeBlock.VERTICAL_FORMS)
{
return true;
}
return false;
}
public static boolean isChineseByBlock(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT)
{
return true;
}
return false;
}
}