博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
调用其他系统接口
阅读量:5881 次
发布时间:2019-06-19

本文共 6977 字,大约阅读时间需要 23 分钟。

//调用接口将数据导入其他系统            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();    }}

 

 

 

转载于:https://www.cnblogs.com/zhuwenxia/p/9186422.html

你可能感兴趣的文章
[翻译]Protocol Buffer 基础: C++
查看>>
runloop与线程的关系
查看>>
[Bzoj2246]迷宫探险(概率+DP)
查看>>
详解消息队列的设计与使用
查看>>
使用Sqoop从mysql向hdfs或者hive导入数据时出现的一些错误
查看>>
控制子窗口的高度
查看>>
处理 Oracle SQL in 超过1000 的解决方案
查看>>
Alpha线性混合实现半透明效果
查看>>
chkconfig 系统服务管理
查看>>
一个简单的运算表达式解释器例子
查看>>
ORACLE---Unit04: SQL(高级查询)
查看>>
Entity Framework Code First 模式-建立多对多联系
查看>>
[LeetCode] Reverse Lists
查看>>
前台页面之<base>标签
查看>>
angular分页插件tm.pagination 解决触发二次请求的问题
查看>>
day08-文件操作
查看>>
教学-45 对象的相等
查看>>
贪食蛇
查看>>
关于Spring 中的事务
查看>>
为什么现在都用面向对象开发,为什么现在都用分层开发结构?
查看>>