金蝶k3c 对接InvokeHelper类的改写
1.问题:InvokeHelper是调用金蝶k3c工具类,因为之前用的是HttpURLConnection 会有个别中午乱码问题,因而采用hutool包的HttpResponse,HttpRequest。
2.解决:之前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;
}
我们把这段代码改为用hutool包调用
private static HttpResponse initUrlConn(String url, JSONArray paras)
throws Exception {
HttpRequest httpRequest = HttpRequest.post(POST_K3CloudURL.concat(url));
if (CookieVal != null) {
httpRequest.header("Cookie", CookieVal);
}
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");
HttpResponse httpResponse=null;
try {
httpResponse = httpRequest.body(jObj.toString(), "application/json;charset=utf-8").timeout(60 * 1000).execute();
} catch (Exception e) {
e.printStackTrace();
}
return httpResponse;
}
3.总结:总的来说用HttpURLConnection调用会有乱码问题,采取hutool包调用就可以解决,至于其中的本质是什么,为什么会出现这样的问题,大家有了解过吗?
还有金蝶k3c 的对接也可以让金蝶方提供 对应的sdk,这样就比较规范。