/** * 查询地方本地的授权用户配置信息,地方端使用 * @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()); }
本地接口调用
于 2024-02-27 10:44:51 首次发布