APIVisit.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import org.apache.http.client.HttpClient;
  2. import org.apache.http.client.methods.HttpGet;
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.HttpStatus;
  5. import org.apache.http.client.utils.URIBuilder;
  6. import org.apache.http.impl.client.HttpClientBuilder;
  7. import org.apache.http.util.EntityUtils;
  8. import java.net.URI;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import net.sf.json.JSONObject;
  12. import javax.crypto.Cipher;
  13. import javax.crypto.spec.SecretKeySpec;
  14. import java.math.BigInteger;
  15. import java.security.MessageDigest;
  16. import java.security.NoSuchAlgorithmException;
  17. import java.security.SecureRandom;
  18. public class APIVisit {
  19. // aesecb加密
  20. private static String AesEcbEncrypt(String plain, String key) {
  21. try {
  22. byte[] keyBytes = key.getBytes("utf-8");
  23. byte[] plainBytes = plain.getBytes("utf-8");
  24. SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
  25. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  26. cipher.init(Cipher.ENCRYPT_MODE, keySpec);
  27. byte[] encrypted = cipher.doFinal(plainBytes);
  28. return bytesToHex(encrypted);
  29. } catch (Exception e) {
  30. System.out.println(e.toString());
  31. return null;
  32. }
  33. }
  34. // aesecb解密
  35. private static String AesEcbDecrypt(String enc, String key) {
  36. try {
  37. byte[] keybytes = key.getBytes("utf-8");
  38. SecretKeySpec keySpec = new SecretKeySpec(keybytes, "AES");
  39. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  40. cipher.init(Cipher.DECRYPT_MODE, keySpec);
  41. byte[] hexbytes = hexToBytes(enc);
  42. byte[] plain = cipher.doFinal(hexbytes);
  43. return new String(plain, "utf-8");
  44. } catch (Exception ex) {
  45. System.out.println(ex.toString());
  46. return null;
  47. }
  48. }
  49. private static String bytesToHex(byte[] data) {
  50. String result="";
  51. for (int i = 0; i < data.length; i++) {
  52. result+=Integer.toHexString((data[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3);
  53. }
  54. return result;
  55. }
  56. public static byte[] hexToBytes(String hexStr) {
  57. char [] data = hexStr.toCharArray();
  58. int len = data.length;
  59. byte[] out = new byte[len >> 1];
  60. for (int i = 0, j = 0; j < len; i++) {
  61. int f = Character.digit(data[j], 16) << 4;
  62. j++;
  63. f = f | Character.digit(data[j], 16);
  64. j++;
  65. out[i] = (byte) (f & 0xFF);
  66. }
  67. return out;
  68. }
  69. // 访问api
  70. public static void visit_by_token(String url, String token, String secret, Boolean enc, Map<String, String> param) {
  71. try {
  72. // http 设置和请求
  73. HttpClient client = HttpClientBuilder.create().build();
  74. URIBuilder uriBuilder = new URIBuilder(url);
  75. if (enc) {
  76. String data = "";
  77. JSONObject jobj = JSONObject.fromObject(param);
  78. data = jobj.toString();
  79. data = AesEcbEncrypt(data, secret);
  80. uriBuilder.setParameter("encrypt_data", data);
  81. } else {
  82. param.entrySet().forEach(item -> {
  83. uriBuilder.setParameter(item.getKey(), item.getValue());
  84. System.out.println("key:" + item.getKey() + ",vaule:" + item.getValue());
  85. });
  86. }
  87. URI reqUrl = uriBuilder.build();
  88. HttpGet request = new HttpGet(reqUrl);
  89. request.setHeader("token", token);
  90. HttpResponse response = client.execute(request);
  91. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  92. String str = EntityUtils.toString(response.getEntity());
  93. System.out.println(str);
  94. JSONObject obj = JSONObject.fromObject(str);
  95. // 数据是否解密
  96. if (obj.containsKey("data")) {
  97. String content = obj.get("data").toString();
  98. if (enc) {
  99. String plaintext = AesEcbDecrypt(content, secret);
  100. System.out.printf("解密后:%s\n", plaintext);
  101. } else {
  102. System.out.printf("不需解密:%s\n", content);
  103. }
  104. }
  105. return;
  106. }
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110. return;
  111. }
  112. public static String md5(String sSrc) {
  113. byte[] secretBytes = null;
  114. try {
  115. MessageDigest md = MessageDigest.getInstance("MD5");
  116. md.update(sSrc.getBytes());
  117. secretBytes = md.digest();
  118. } catch (NoSuchAlgorithmException e) {
  119. System.out.println(e.toString());
  120. return null;
  121. }
  122. String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字
  123. Integer l = md5code.length();
  124. for (int i = 0; i < 32 - l; i++) {
  125. md5code = "0" + md5code;
  126. }
  127. return md5code;
  128. }
  129. public static String getSign(String ts, String appKey, String appSecrect) {
  130. String s = appKey + appSecrect + ts;
  131. s = md5(s);
  132. return s;
  133. }
  134. public static void visit_by_sign(String url, String appkey, String secret, Boolean enc, Map<String, String> param) {
  135. try {
  136. // http 设置和请求
  137. HttpClient client = HttpClientBuilder.create().build();
  138. URIBuilder uriBuilder = new URIBuilder(url);
  139. if (enc) {
  140. String data = "";
  141. JSONObject jobj = JSONObject.fromObject(param);
  142. data = jobj.toString();
  143. data = AesEcbEncrypt(data, secret);
  144. uriBuilder.setParameter("encrypt_data", data);
  145. } else {
  146. param.entrySet().forEach(item -> {
  147. uriBuilder.setParameter(item.getKey(), item.getValue());
  148. System.out.println("key:" + item.getKey() + ",vaule:" + item.getValue());
  149. });
  150. }
  151. String ts = Long.toString(System.currentTimeMillis());
  152. String sign = getSign(ts, appkey, secret);
  153. URI reqUrl = uriBuilder.build();
  154. HttpGet request = new HttpGet(reqUrl);
  155. request.setHeader("appkey", appkey);
  156. request.setHeader("timestamp", ts);
  157. request.setHeader("sign", sign);
  158. HttpResponse response = client.execute(request);
  159. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  160. String str = EntityUtils.toString(response.getEntity());
  161. System.out.println(str);
  162. JSONObject obj = JSONObject.fromObject(str);
  163. // 数据是否解密
  164. if (obj.containsKey("data")) {
  165. String content = obj.get("data").toString();
  166. if (enc) {
  167. String plaintext = AesEcbDecrypt(content, secret);
  168. System.out.printf("解密后:%s\n", plaintext);
  169. } else {
  170. System.out.printf("不需解密:%s\n", content);
  171. }
  172. }
  173. return;
  174. }
  175. } catch (Exception e) {
  176. e.printStackTrace();
  177. }
  178. return;
  179. }
  180. public static String getToken(String tokenUrl, String user, String password) {
  181. try {
  182. HttpClient client = HttpClientBuilder.create().build();
  183. URIBuilder uriBuilder = new URIBuilder(tokenUrl);
  184. uriBuilder.setParameter("user", user);
  185. uriBuilder.setParameter("password", password);
  186. URI reqUrl = uriBuilder.build();
  187. HttpGet request = new HttpGet(reqUrl);
  188. HttpResponse response = client.execute(request);
  189. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  190. //读取返回
  191. String str = EntityUtils.toString(response.getEntity());
  192. System.out.printf("token result:%s\n", str);
  193. JSONObject obj = JSONObject.fromObject(str);
  194. //读取token
  195. if (obj.containsKey("token")) {
  196. return obj.get("token").toString();
  197. }
  198. return "";
  199. }
  200. } catch (Exception e) {
  201. e.printStackTrace();
  202. }
  203. return "";
  204. }
  205. public static void main(String[] args) throws Exception {
  206. // 配置请联系运营商获取
  207. // 实际接口url
  208. String apiUrl = "http://127.0.0.1:41002/api/v1/query/2001";
  209. // token url
  210. String tokenUrl = "http://127.0.0.1:41002/api/v1/token";
  211. // api 用户名和密码用于获取token
  212. String user = "529db83441acff61a054eba562185515";
  213. String password = "DMh7lbyv";
  214. // 密钥用于加密数据(非加密接口可不用)
  215. String secret = "1749f2a7019db090ca7c9cc69e64033c";
  216. // 是否加密
  217. Boolean is_enc = true;
  218. Boolean use_token = false;
  219. // 调用接口
  220. HashMap<String, String> param = new HashMap<>();
  221. // 接口参数根据实际api接口填写
  222. param.put("plate_no", "川Y01M21");
  223. param.put("plate_type", "02");
  224. param.put("owner", "吴军");
  225. if (use_token) {
  226. // 获取token
  227. String token = getToken(tokenUrl, user, password);
  228. if (token == "") {
  229. System.out.println("token is empty");
  230. return;
  231. }
  232. visit_by_token(apiUrl, token, secret, is_enc, param);
  233. } else {
  234. visit_by_sign(apiUrl, user, secret, is_enc, param);
  235. }
  236. }
  237. }