package tqq9.lc123.cloud.app.plugin.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import okhttp3.*; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.sql.Timestamp; import java.util.*; public class FWRestfulUtils { /** *restful接口调用案例 *以getModeDataPageList为例 */ public String doAction(String url, JSONObject mainTable, String fwuserid){ String thirdConfigByNumber = ConfigUtils.getThirdConfigByNumber("FW_Main_URL");//获取泛微接口ip、端口号 String systemid = ConfigUtils.getThirdConfigByNumber("FW_systemid");//获取泛微接口授权用户名、系统标识 String d_password = ConfigUtils.getThirdConfigByNumber("FW_password");//获取泛微接口授权密码 CloseableHttpResponse response;// 响应类, CloseableHttpClient httpClient = HttpClients.createDefault(); //restful接口url HttpPost httpPost = new HttpPost(thirdConfigByNumber + url); //当前日期 String currentDate = getCurrentDate(); //当前时间 String currentTime = getCurrentTime(); //获取时间戳 String currentTimeTamp = getTimestamp(); Map params = new HashMap<>(); Map dataParam = new HashMap<>(); Map datajson = new HashMap<>(); //header Map header = new HashMap<>(); header.put("systemid", systemid); header.put("currentDateTime", currentTimeTamp); String md5Source = systemid + d_password + currentTimeTamp; String md5OfStr = getMD5Str(md5Source).toLowerCase(); //Md5是:系统标识+密码+时间戳 并且md5加密的结果 header.put("Md5",md5OfStr); //封装pageinfo // JSONObject pageInfo = new JSONObject(); // pageInfo.put("pageNo", 1); // pageInfo.put("pageSize", 10); // dataParam.put("pageInfo",pageInfo); //封装mainTable参数 dataParam.put("mainTable",mainTable); //封装operationinfo参数 JSONObject operationinfo = new JSONObject(); operationinfo.put("operator", fwuserid); dataParam.put("operationinfo",operationinfo); System.out.println("===请求参数dataparam==="+dataParam); params.put("data",dataParam); Map[] arr = new Map[1]; arr[0] = dataParam; datajson.put("header", header); datajson.put("data", arr); //装填参数 List list = new ArrayList<>(); list.add(new BasicNameValuePair("datajson", JSON.toJSONString(datajson))); try{ httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8"); httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8")); response = httpClient.execute(httpPost); if (response != null && response.getEntity() != null) { //返回信息 String resulString = EntityUtils.toString(response.getEntity()); System.out.println("成功"+ resulString); return resulString; }else{ System.out.println("获取数据失败,请查看日志"+currentDate+" "+currentTime); } }catch (Exception e){ System.out.println("请求失败"+currentDate+" "+currentTime+"====errormsg:"+e.getMessage()); } return null; } public String getMD5Str(String plainText){ //定义一个字节数组 byte[] secretBytes = null; try { // 生成一个MD5加密计算摘要 MessageDigest md = MessageDigest.getInstance("MD5"); //对字符串进行加密 md.update(plainText.getBytes()); //获得加密后的数据 secretBytes = md.digest(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("没有md5这个算法!"); // throw new RuntimeException(SystemEnv.getHtmlLabelName(517545,userLanguage)); } //将加密后的数据转换为16进制数字 String md5code = new BigInteger(1, secretBytes).toString(16); // 如果生成数字未满32位,需要前面补0 // 不能把变量放到循环条件,值改变之后会导致条件变化。如果生成30位 只能生成31位md5 int tempIndex = 32 - md5code.length(); for (int i = 0; i < tempIndex; i++) { md5code = "0" + md5code; } return md5code; } public static String getCurrentTime() { Date newdate = new Date(); long datetime = newdate.getTime(); Timestamp timestamp = new Timestamp(datetime); String currenttime = (timestamp.toString()).substring(11, 13) + ":" + (timestamp.toString()).substring(14, 16) + ":" + (timestamp.toString()).substring(17, 19); return currenttime; } public static String getCurrentDate() { Date newdate = new Date(); long datetime = newdate.getTime(); Timestamp timestamp = new Timestamp(datetime); String currentdate = (timestamp.toString()).substring(0, 4) + "-" + (timestamp.toString()).substring(5, 7) + "-" + (timestamp.toString()).substring(8, 10); return currentdate; } /** * 获取当前日期时间。 YYYY-MM-DD HH:MM:SS * @return 当前日期时间 */ public static String getCurDateTime() { Date newdate = new Date(); long datetime = newdate.getTime(); Timestamp timestamp = new Timestamp(datetime); return (timestamp.toString()).substring(0, 19); } /** * 获取时间戳 格式如:19990101235959 * @return */ public static String getTimestamp(){ return getCurDateTime().replace("-", "").replace(":", "").replace(" ", ""); } public static int getIntValue(String v, int def) { try { return Integer.parseInt(v); } catch (Exception ex) { return def; } } public static String null2String(Object s) { return s == null ? "" : s.toString(); } }