金蝶接口调用步骤

/**
 * 获取应用令牌
 *
 * @param map
 * @return
 * @throws IOException
 */
private InvoicingTokenResultVo getAppToken(Map<String, Object> map) throws IOException {
    //1. 允许第三方系统通过 API 得到应用 Token。
    String url= urlStr+"/ierp/api/getAppToken.do";
    // 参数
    InvoicingTokenVO invoicingTokenVO = new InvoicingTokenVO();
    invoicingTokenVO.setAppId(appId);
    invoicingTokenVO.setAppSecuret(appSecuret);
    invoicingTokenVO.setAccountId(accountId);
    invoicingTokenVO.setTenantid(tenantid);
    invoicingTokenVO.setLanguage(language);
    String accessToken = JSONObject.toJSONString(invoicingTokenVO);
    // 发送请求
    String resultJson = HttpUtil.doPostJsonTokeen(url, accessToken);
    JSONObject jsonObject1 = JSONObject.parseObject(resultJson);
    String data = jsonObject1.get("data").toString();
    ObjectMapper mapper = new ObjectMapper();
    InvoicingTokenResultVo invoicingTokenResultVo= mapper.readValue(data, InvoicingTokenResultVo.class);
    return invoicingTokenResultVo;
}
/**
 * 获取用户令牌
 *
 * @param tokenResultVo
 * @return
 * @throws IOException
 */
private InvoicingAccessTokenResultVO getAccessToken(InvoicingTokenResultVo tokenResultVo) throws IOException {
    String url=urlStr+"/ierp/api/login.do";
    InvoicingAccessTokenVO invoicingAccessTokenVO =new InvoicingAccessTokenVO();
    invoicingAccessTokenVO.setUser(userName);
    invoicingAccessTokenVO.setApptoken(tokenResultVo.getApp_token());
    invoicingAccessTokenVO.setAccountId(accountId);
    invoicingAccessTokenVO.setTenantid(tenantid);
    invoicingAccessTokenVO.setUsertype(usertype);
    invoicingAccessTokenVO.setLanguage(language);
    String accessToken = JSONObject.toJSONString(invoicingAccessTokenVO);
    // 发送请求
    String resultJson = HttpUtil.doPostJsonTokeen(url, accessToken);
    JSONObject jsonObject1 = JSONObject.parseObject(resultJson);
    String data = jsonObject1.get("data").toString();
    ObjectMapper mapper = new ObjectMapper();
    InvoicingAccessTokenResultVO tokenResult= mapper.readValue(data, InvoicingAccessTokenResultVO.class);
    return tokenResult;
}
/**
 * http post请求,Content-Type为json
 *
 * @param httpUrl 请求地址
 * @param param   请求参数(json)
 * @return 返回结果
 */
public static String doPostJsonTokeen(String httpUrl, @Nullable String param) {
    logger.info("发送post请求参数:{},地址:{}",param,httpUrl);
    HttpURLConnection connection = null;
    InputStream is = null;
    OutputStream os = null;
    BufferedReader br = null;
    StringBuffer result = new StringBuffer();
    try {
        //创建连接对象
        URL url = new URL(httpUrl);
        //创建链接
        connection = (HttpURLConnection) url.openConnection();
        //设置请求方法
        connection.setRequestMethod("POST");
        //设置连接超时时间(毫秒)
        connection.setConnectTimeout(15000);
        //设置读取超时时间
        connection.setReadTimeout(15000);
        //DoOutput设置是否想httpUrlConnection输出,DoInput设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个值
        //设置是否可读取
        connection.setDoInput(true);
        connection.setDoOutput(true);
        //设置通用的请求属性
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        //拼装参数
        if (!TextUtils.isBlank(param)) {
            //设置参数
            os = connection.getOutputStream();
            //拼装参数
            os.write(param.getBytes("UTF-8"));
        }
        //设置权限
        //设置请求头
        //开启连接
        connection.connect();
        //读取响应
        if (connection.getResponseCode() == 200) {
            is = connection.getInputStream();
            if (null != is) {
                br = new BufferedReader(new InputStreamReader(is, "utf-8"));
                String temp = null;
                while (null != (temp = br.readLine())) {
                    result.append(temp);
                }
            }
        }
    logger.info("返回的参数:{}",result);
    } catch (IOException e) {
        logger.info("请求错误--" + e.getMessage());
        e.printStackTrace();
    } finally {
        if (null != br) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != os) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //关闭远程连接
        connection.disconnect();
    }
    return result.toString();
}

相关推荐

  1. 接口调用步骤

    2024-01-06 07:44:06       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-06 07:44:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-06 07:44:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-06 07:44:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-06 07:44:06       18 阅读

热门阅读

  1. 网页多文件合并下载成zip

    2024-01-06 07:44:06       40 阅读
  2. Micropython的包管理

    2024-01-06 07:44:06       43 阅读
  3. this的使用(js的问题)

    2024-01-06 07:44:06       39 阅读
  4. LeetCode每日一题 | 1944. 队列中可以看到的人数

    2024-01-06 07:44:06       40 阅读
  5. 1.2 C#基础

    2024-01-06 07:44:06       38 阅读
  6. PHP篇——html+php实现表单提交的一个简单例子

    2024-01-06 07:44:06       41 阅读
  7. Spring Boot 和 Spring Framework 的区别

    2024-01-06 07:44:06       48 阅读
  8. Spring Boot 生产就绪中文文档-下

    2024-01-06 07:44:06       30 阅读
  9. TensorFlow的详细介绍

    2024-01-06 07:44:06       33 阅读
  10. Android设备sdcard/tf卡不识别在电脑上可以

    2024-01-06 07:44:06       39 阅读
  11. 记一次 easyswoole 热重载失效复盘 grpc扩展惹的祸

    2024-01-06 07:44:06       48 阅读