APIVisit.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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(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("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 getToken(String tokenUrl, String user, String password) {
  113. try {
  114. HttpClient client = HttpClientBuilder.create().build();
  115. URIBuilder uriBuilder = new URIBuilder(tokenUrl);
  116. uriBuilder.setParameter("user", user);
  117. uriBuilder.setParameter("password", password);
  118. URI reqUrl = uriBuilder.build();
  119. HttpGet request = new HttpGet(reqUrl);
  120. HttpResponse response = client.execute(request);
  121. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  122. //读取返回
  123. String str = EntityUtils.toString(response.getEntity());
  124. System.out.printf("token result:%s\n", str);
  125. JSONObject obj = JSONObject.fromObject(str);
  126. //读取token
  127. if (obj.containsKey("token")) {
  128. return obj.get("token").toString();
  129. }
  130. return "";
  131. }
  132. } catch (Exception e) {
  133. e.printStackTrace();
  134. }
  135. return "";
  136. }
  137. public static void main(String[] args) throws Exception {
  138. // 配置请联系运营商获取
  139. // 实际接口url
  140. String apiUrl = "";
  141. // token url
  142. String tokenUrl = "";
  143. // api 用户名和密码用于获取token
  144. String user = "";
  145. String password = "";
  146. // 密钥用于加密数据(非加密接口可不用)
  147. String appSecret = "";
  148. // 是否加密
  149. Boolean is_enc = true;
  150. // 获取token
  151. String token = getToken(tokenUrl, user, password);
  152. if (token == "") {
  153. System.out.println("token is empty");
  154. return;
  155. }
  156. // 调用接口
  157. HashMap<String, String> param = new HashMap<>();
  158. // 接口参数根据实际api接口填写
  159. param.put("", "");
  160. visit(apiUrl, token, appSecret, is_enc, param);
  161. }
  162. }