本地接口调用

/**
 * 查询地方本地的授权用户配置信息,地方端使用
 * @return
 */
public static Map<String,String> queryLocalVistorConfig() throws DebtException {
    BgdDataSource bgdDataSource = (BgdDataSource) SpringContextUtil.getApplicationContext().getBean("bgdDataSource");
    //20210301 fzd 安全代码修改,将password改为psd
    String sql = "SELECT 'DF' AS TYPE_ID,AD_CODE,USER_CODE U_CODE,PASSWORD PDD,ENC_SF,ENC_KEY,IP_ADDRESS,IP_MAIN,PORT,DOMAIN_NAME,F_PUB_KEY,F_PRI_KEY,S_PUB_KEY,S_PRI_KEY FROM DEBT_T_BDJH_CONFIG_VISITOR T WHERE T.IS_VALID = '1'";
    Map map = bgdDataSource.findBySqlMap(sql);
    if(map.size()<=0){
        throw new DebtException("本地用户配置不符合规则!");
    }
    return addRequestURL(map);
}

/**
 * 添加本地区划,财政部为87,地方为自己省级区划
 * @param map
 * @return
 */
private static Map<String,String> addRequestURL(Map map){
    //拼接ip地址
    String ip_main = MapUtils.getString(map, "IP_MAIN");
    String port = MapUtils.getString(map, "PORT");
    String domain_name = MapUtils.getString(map, "DOMAIN_NAME");
    StringBuffer url = new StringBuffer("http://");
    //20220125 chenfei 代码同步
    System.out.println(map);
    url.append(ip_main);
    if(!StringTool.isNull(port)){
        url.append(":").append(port);
    }
    if(domain_name.startsWith("/")){
        url.append(domain_name);
    }else {
        url.append("/").append(domain_name);
    }
    map.put("URL",url);
    return map;
}

/**
 * 请求方法
 * @param param 请求参数
 * @param sjType 请求类型(TS 数据推送;JS 结果接收)
 * @return
 */
public static Map sendPostRequest(JSONObject param, String sjType) {
    System.out.println(param);
    StringBuilder responseData = new StringBuilder();
    HttpURLConnection servletConnection = null;
    OutputStream responseOutput = null;
    InputStream inputStream = null;
    Reader inputStreamReader = null;
    BufferedReader bufferedReader = null;
    try {
        Map<String, String> userConfigMap = DBUtil.queryLocalVistorConfig();
        SM4Utils sm4 = new SM4Utils();
        String ipPort = ServerUtil.getIpPort();
        String value = sm4.encryptData_ECB(StringTool.getValueFromMapByKey(userConfigMap,"U_CODE")+"#"+
                StringTool.getValueFromMapByKey(userConfigMap,"PDD"));
        String ip_address = StringTool.getValueFromMapByKey(userConfigMap,"IP_ADDRESS");
        String ip_port = StringTool.getValueFromMapByKey(userConfigMap,"PORT");
        servletConnection = (HttpURLConnection) (new URL("http://"+ip_address+":"+ip_port+"/dataTransferServlet")).openConnection();
        //servletConnection = (HttpURLConnection) (new URL("http://192.168.1.96:8080/dataTransferServlet")).openConnection();
        servletConnection.setRequestMethod("POST");
        servletConnection.setConnectTimeout(120000); // 设置连接超时为120s
        servletConnection.setReadTimeout(120000); // 读取数据超时也是120s
        servletConnection.setRequestProperty("Content-Type", "application/json"); // 设置请求头
        if("TS".equals(sjType)) {// 设置请求类型
            servletConnection.setRequestProperty("type", "projectcheck"); // 数据推送
        }else{
            servletConnection.setRequestProperty("type", "projectquery"); // 结果接收
        }
        servletConnection.setRequestProperty("Referer", ipPort);
        servletConnection.setRequestProperty("adcode", StringTool.getValueFromMapByKey(userConfigMap,"AD_CODE"));
        servletConnection.setRequestProperty("secrity_token", value);
        servletConnection.setDoOutput(true);
        responseOutput = servletConnection.getOutputStream();
        responseOutput.write(param.toString().getBytes(StandardCharsets.UTF_8));
        int responseCode = servletConnection.getResponseCode();
        if(HttpStatus.SC_OK != responseCode) {
            throw new RuntimeException("数据交互接口请求失败");
        }
        inputStream = servletConnection.getInputStream();
        inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        bufferedReader = new BufferedReader(inputStreamReader);
        String inputLine = null;
        while ((inputLine = bufferedReader.readLine()) != null) {
            responseData.append(inputLine);
        }
    }catch (Exception e) {
        System.out.println("发改委接口日志:获取token信息失败");
        e.printStackTrace();
    } finally {
        try {
            if(bufferedReader != null) {
                bufferedReader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if(inputStreamReader != null) {
                inputStreamReader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if(inputStream != null) {
                inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if(responseOutput != null) {
                responseOutput.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        servletConnection.disconnect();
    }
    return JSONObject.fromObject(responseData.toString());
}
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值