InvokeHelper.java
5.61 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
package com.huaheng.api.jindie;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huaheng.api.jindie.domain.uri.JinDieApiUri;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
@Service
public class InvokeHelper {
public static String POST_K3CloudURL = "http://219.137.250.36:8999/K3Cloud/";
// Cookie 值
private static String CookieVal = null;
// HttpURLConnection
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;
}
// Login
public static boolean Login(String acctID, String username, String password, int Lcid)
throws Exception {
boolean bResult = false;
String sUrl = JinDieApiUri.LOGIN.getApiUri();
JSONArray jParas = new JSONArray();
jParas.add(acctID);// 帐套Id
jParas.add(username);// 用户名
jParas.add(password);// 密码
jParas.add(Lcid);// 语言
HttpURLConnection connection = initUrlConn(sUrl, jParas);
// 获取Cookie
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()));
String line;
while ((line = reader.readLine()) != null) {
String sResult = new String(line.getBytes(), "utf-8");
System.out.println(sResult);
bResult = line.contains("LoginResultType");
}
reader.close();
connection.disconnect();
return bResult;
}
/**
* 采购入库单操作
* @param formId 单号
* @param content 内容
* @throws Exception
*/
// Push
public static void Push(String formId, String content) throws Exception {
Invoke("Push", formId, content);
}
// Select
public static void Select(String formId, String content) throws Exception {
Invoke("Select", formId, content);
}
// Batch_Save
public static void Batch_Save(String formId, String content) throws Exception {
Invoke("Batch_Save", formId, content);
}
// Submit
public static void Submit(String formId, String content) throws Exception {
Invoke("Submit", formId, content);
}
// Audit
public static void Audit(String formId, String content) throws Exception {
Invoke("Audit", formId, content);
}
private static void Invoke(String deal, String formId, String content)
throws Exception {
String sUrl = deal;
JSONArray jParas = new JSONArray();
jParas.add(formId);
jParas.add(content);
HttpURLConnection connectionInvoke = initUrlConn(sUrl, jParas);
BufferedReader reader = new BufferedReader(new InputStreamReader(
connectionInvoke.getInputStream()));
String line;
System.out.println(" ============================= ");
System.out.println(" Contents of post request ");
System.out.println(" ============================= ");
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();
}
/**
* 把中文转成Unicode码
*
* @param str
* @return
*/
public static String chinaToUnicode(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
int chr1 = (char) str.charAt(i);
if (chr1 >= 19968 && chr1 <= 171941) {// 汉字范围 \u4e00-\u9fa5 (中文)
result += "\\u" + Integer.toHexString(chr1);
} else {
result += str.charAt(i);
}
}
return result;
}
}