123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- 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(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("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 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 = "";
- // token url
- String tokenUrl = "";
- // api 用户名和密码用于获取token
- String user = "";
- String password = "";
- // 密钥用于加密数据(非加密接口可不用)
- String appSecret = "";
- // 是否加密
- Boolean is_enc = true;
- // 获取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("", "");
- visit(apiUrl, token, appSecret, is_enc, param);
- }
- }
|