123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- 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<String, String> 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<String, String> 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<String, String> 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);
- }
- }
- }
|