import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import java.net.URI; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; public class APIVisit { // aesecb加密 private static String AesEcbEncrypt(String plain, String key) { try { byte[] keyBytes = key.getBytes("utf-8"); byte[] plainBytes = plain.getBytes("utf-8"); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] encrypted = cipher.doFinal(plainBytes); return bytesToHex(encrypted); } catch (Exception e) { System.out.println(e.toString()); return null; } } // aesecb解密 private static String AesEcbDecrypt(String enc, String key) { try { byte[] keybytes = key.getBytes("utf-8"); SecretKeySpec keySpec = new SecretKeySpec(keybytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] hexbytes = hexToBytes(enc); byte[] plain = cipher.doFinal(hexbytes); return new String(plain, "utf-8"); } catch (Exception ex) { System.out.println(ex.toString()); return null; } } private static String bytesToHex(byte[] data) { String result=""; for (int i = 0; i < data.length; i++) { result+=Integer.toHexString((data[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3); } return result; } public static byte[] hexToBytes(String hexStr) { char [] data = hexStr.toCharArray(); int len = data.length; byte[] out = new byte[len >> 1]; for (int i = 0, j = 0; j < len; i++) { int f = Character.digit(data[j], 16) << 4; j++; f = f | Character.digit(data[j], 16); j++; out[i] = (byte) (f & 0xFF); } return out; } // 访问api public static void visit_by_token(String url, String token, String secret, Boolean enc, Map param) { try { // http 设置和请求 HttpClient client = HttpClientBuilder.create().build(); URIBuilder uriBuilder = new URIBuilder(url); if (enc) { String data = ""; JSONObject jobj = JSONObject.fromObject(param); data = jobj.toString(); data = AesEcbEncrypt(data, secret); uriBuilder.setParameter("encrypt_data", data); } else { param.entrySet().forEach(item -> { uriBuilder.setParameter(item.getKey(), item.getValue()); System.out.println("key:" + item.getKey() + ",vaule:" + item.getValue()); }); } URI reqUrl = uriBuilder.build(); HttpGet request = new HttpGet(reqUrl); request.setHeader("token", token); HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String str = EntityUtils.toString(response.getEntity()); System.out.println(str); JSONObject obj = JSONObject.fromObject(str); // 数据是否解密 if (obj.containsKey("data")) { String content = obj.get("data").toString(); if (enc) { String plaintext = AesEcbDecrypt(content, secret); System.out.printf("解密后:%s\n", plaintext); } else { System.out.printf("不需解密:%s\n", content); } } return; } } catch (Exception e) { e.printStackTrace(); } return; } public static String md5(String sSrc) { byte[] secretBytes = null; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(sSrc.getBytes()); secretBytes = md.digest(); } catch (NoSuchAlgorithmException e) { System.out.println(e.toString()); return null; } String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字 Integer l = md5code.length(); for (int i = 0; i < 32 - l; i++) { md5code = "0" + md5code; } return md5code; } public static String getSign(String ts, String appKey, String appSecrect) { String s = appKey + appSecrect + ts; s = md5(s); return s; } public static void visit_by_sign(String url, String appkey, String secret, Boolean enc, Map param) { try { // http 设置和请求 HttpClient client = HttpClientBuilder.create().build(); URIBuilder uriBuilder = new URIBuilder(url); if (enc) { String data = ""; JSONObject jobj = JSONObject.fromObject(param); data = jobj.toString(); data = AesEcbEncrypt(data, secret); uriBuilder.setParameter("encrypt_data", data); } else { param.entrySet().forEach(item -> { uriBuilder.setParameter(item.getKey(), item.getValue()); System.out.println("key:" + item.getKey() + ",vaule:" + item.getValue()); }); } String ts = Long.toString(System.currentTimeMillis()); String sign = getSign(ts, appkey, secret); URI reqUrl = uriBuilder.build(); HttpGet request = new HttpGet(reqUrl); request.setHeader("appkey", appkey); request.setHeader("timestamp", ts); request.setHeader("sign", sign); HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String str = EntityUtils.toString(response.getEntity()); System.out.println(str); JSONObject obj = JSONObject.fromObject(str); // 数据是否解密 if (obj.containsKey("data")) { String content = obj.get("data").toString(); if (enc) { String plaintext = AesEcbDecrypt(content, secret); System.out.printf("解密后:%s\n", plaintext); } else { System.out.printf("不需解密:%s\n", content); } } return; } } catch (Exception e) { e.printStackTrace(); } return; } public static String getToken(String tokenUrl, String user, String password) { try { HttpClient client = HttpClientBuilder.create().build(); URIBuilder uriBuilder = new URIBuilder(tokenUrl); uriBuilder.setParameter("user", user); uriBuilder.setParameter("password", password); URI reqUrl = uriBuilder.build(); HttpGet request = new HttpGet(reqUrl); HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { //读取返回 String str = EntityUtils.toString(response.getEntity()); System.out.printf("token result:%s\n", str); JSONObject obj = JSONObject.fromObject(str); //读取token if (obj.containsKey("token")) { return obj.get("token").toString(); } return ""; } } catch (Exception e) { e.printStackTrace(); } return ""; } public static void main(String[] args) throws Exception { // 配置请联系运营商获取 // 实际接口url String apiUrl = "http://127.0.0.1:41002/api/v1/query/2001"; // token url String tokenUrl = "http://127.0.0.1:41002/api/v1/token"; // api 用户名和密码用于获取token String user = "529db83441acff61a054eba562185515"; String password = "DMh7lbyv"; // 密钥用于加密数据(非加密接口可不用) String secret = "1749f2a7019db090ca7c9cc69e64033c"; // 是否加密 Boolean is_enc = true; Boolean use_token = false; // 调用接口 HashMap param = new HashMap<>(); // 接口参数根据实际api接口填写 param.put("plate_no", "川Y01M21"); param.put("plate_type", "02"); param.put("owner", "吴军"); if (use_token) { // 获取token String token = getToken(tokenUrl, user, password); if (token == "") { System.out.println("token is empty"); return; } visit_by_token(apiUrl, token, secret, is_enc, param); } else { visit_by_sign(apiUrl, user, secret, is_enc, param); } } }