调用其他系统接口

Posted zhuwenxia

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用其他系统接口相关的知识,希望对你有一定的参考价值。

技术分享图片

//调用接口将数据导入其他系统
            JSONObject reqContent;
            String url;
            String flag = null;
            reqContent = new JSONObject();
            reqContent.put("creditCode",moduleRecord.getProductorCreditCode());
            reqContent.put("module_Pattern", moduleRecord.getModuleModel());
            JSONObject tempJson = new JSONObject();
            tempJson.put("monomer_Pattern",moduleRecord.getMonomerIncluded());
            tempJson.put("count",Double.parseDouble(moduleRecord.getMonomerIncludedNum()));
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(tempJson);
            reqContent.put("monomerItems", jsonArray);
            url = "http://222.217.61.58:10000/sgmw-btms-api/batterydata/v1/addModuleRecord";
            flag =SGMWUtil.insertDataToSgmw(url,reqContent,SGMWUtil.authKey_MR,SGMWUtil.authPWD_MR);
            

 

SGMWUtil.java
public class SGMWUtil {
    //上传企业基本信息
    public static String authKey_Por = "6DEF9596FB92DB5BE053360116AC258C";
    public static String authPWD_Por = "6DEF9596FB93DB5BE053360116AC258C";
    //上传电池单体备案数据
    public static String authKey_MnR = "6DEF8492F4CED9F2E053360116ACFF11";
    public static String authPWD_MnR = "6DEF8492F4CFD9F2E053360116ACFF11";
    public static JSONObject httpInterfaceForJson(String requestUrl, String requestMethod, JSONObject fullJson) {
        String res = "";
        StringBuffer buffer = new StringBuffer();
        HttpURLConnection httpUrlConn = null;
        try {
            URL url = new URL(requestUrl);
            httpUrlConn = (HttpURLConnection) url.openConnection();
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
//            httpUrlConn.setRequestProperty("Accept", "text/plain");
            httpUrlConn.setRequestProperty("Content-Type", "application/json");
            httpUrlConn.setRequestMethod(requestMethod);
            httpUrlConn.getOutputStream().write(fullJson.toString().getBytes("UTF-8"));
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            while((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            res = buffer.toString();
        } catch (ConnectException var21) {
        } catch (Exception var22) {
        } finally {
            try {
                httpUrlConn.disconnect();
            } catch (Exception var20) {
            }
        }
        return JSONObject.parseObject(res);
    }

    public static String encodeMD5(String str){
        String strDigest = "";
        try{
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] data = md5.digest(str.getBytes("utf-8"));
            strDigest = bytesToHexString(data);
        } catch (Exception ex){
            throw new RuntimeException(ex);
        }
        return strDigest;
    }

    public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if(src == null || src.length <= 0){
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if(hv.length() < 2){
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static String encrypt(String content,String password){
        try{
            if(StringUtils.isEmpty(content))
                return "";
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(password.getBytes());
            kgen.init(128,random);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat,"AES");
            Cipher cipher = Cipher.getInstance("AES");
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(byteContent);
            String str = Base64.getEncoder().encodeToString(result);
            return str;
        } catch (NoSuchAlgorithmException e) {
        } catch (NoSuchPaddingException e){
        } catch (InvalidKeyException e) {
        } catch (UnsupportedEncodingException e){
        } catch (IllegalBlockSizeException e){
        } catch (BadPaddingException e) {
        }
        return null;
    }

    public static String decrypt(String str,String password){
        try{
            byte[] content = Base64.getDecoder().decode(str);
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(password.getBytes());
            kgen.init(128,secureRandom);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat,"AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE,key);
            byte[] result = cipher.doFinal(content);
            return new String(result,"UTF-8");
        } catch (NoSuchAlgorithmException e){
        } catch (NoSuchPaddingException e){
        } catch (InvalidKeyException e){
        } catch (IllegalBlockSizeException e){
        } catch (BadPaddingException e){
        } catch (Exception e){
        }
        return "";
    }

    public static String insertDataToSgmw(String url,JSONObject reqContent,String authKey,String authPWD){
        //接口调用
        String reqGUID = BaseUtil.getUUID();
        JSONObject reqHead = new JSONObject();
        reqHead.put("authKey",authKey);
        reqHead.put("encryptMode","AES");
        reqHead.put("reqGUID",reqGUID);
        reqHead.put("reqSign",encodeMD5(authKey+"|"+authPWD+"|"+reqGUID));
        JSONObject reqBody = new JSONObject();
        reqBody.put("reqContent",encrypt(reqContent.toString(),authPWD));
        JSONObject fullJson = new JSONObject();
        fullJson.put("reqHead",reqHead);
        fullJson.put("reqBody",reqBody);
        JSONObject jsonObject = httpInterfaceForJson(url,
                "POST", fullJson);
        if(jsonObject == null || jsonObject.get("rspHead") == null || JSONObject.parseObject(jsonObject.get("rspHead").toString()).get("rspCode") == null){
            return "0";
        }
        return JSONObject.parseObject(jsonObject.get("rspHead").toString()).get("rspCode").toString();
    }

}

 

 

 

以上是关于调用其他系统接口的主要内容,如果未能解决你的问题,请参考以下文章

调用其他系统接口

SpringBoot项目使用hutool工具进行HttpClient接口调用的处理(文件上传)

从其他片段添加新的 RecyclerView 项

仅在一个片段中隐藏状态栏并在其他片段中显示

尝试在空对象引用上调用接口方法“____”[重复]

JMeter接口测试-模块控制器