Parcourir la source

sign or token

jason il y a 1 an
Parent
commit
5a330142e5
1 fichiers modifiés avec 103 ajouts et 17 suppressions
  1. 103 17
      APIVisit.java

+ 103 - 17
APIVisit.java

@@ -74,7 +74,7 @@ public class APIVisit {
                  return out;
              }
     // 访问api
-    public static void visit(String url, String token, String secret, Boolean enc, Map<String, String> param) {
+    public static void visit_by_token(String url, String token, String secret, Boolean enc, Map<String, String> param) {
 
         try {
            
@@ -86,7 +86,7 @@ public class APIVisit {
                 JSONObject jobj = JSONObject.fromObject(param);
                 data = jobj.toString();
                 data = AesEcbEncrypt(data, secret);
-                uriBuilder.setParameter("data", data);
+                uriBuilder.setParameter("encrypt_data", data);
             } else {
                 param.entrySet().forEach(item -> {
                         uriBuilder.setParameter(item.getKey(), item.getValue());
@@ -124,6 +124,85 @@ public class APIVisit {
         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 {
 
@@ -162,30 +241,37 @@ public class APIVisit {
     public static void main(String[] args) throws Exception {
         // 配置请联系运营商获取
         // 实际接口url
-        String apiUrl = "";
+       String apiUrl = "http://127.0.0.1:41002/api/v1/query/2001";
         // token url
-        String tokenUrl = "";
+        String tokenUrl = "http://127.0.0.1:41002/api/v1/token";
         // api 用户名和密码用于获取token
-        String user = "";
-        String password = "";
+        String user = "529db83441acff61a054eba562185515";
+        String password = "DMh7lbyv";
         // 密钥用于加密数据(非加密接口可不用)
-        String appSecret = "";
+        String secret = "1749f2a7019db090ca7c9cc69e64033c";
         // 是否加密
         Boolean is_enc = true;
+	Boolean use_token = false;
 
 
-
-	// 获取token
-        String token = getToken(tokenUrl, user, password);
-        if (token == "") {
-            System.out.println("token is empty");
-            return;
-        }
 	// 调用接口
         HashMap<String, String> param = new HashMap<>();
         // 接口参数根据实际api接口填写
-        param.put("", "");
+	param.put("plate_no", "川Y01M21");
+	param.put("plate_type", "02");
+	param.put("owner", "吴军");
 
-         visit(apiUrl, token, appSecret, is_enc, param);
-        }
+	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);
+	}
     }
+}