金蝶k3c 对接InvokeHelper类的改写

金蝶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,这样就比较规范。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值