mitao
2024-04-22 28a19584dca02f05fe0b8be9ab426b586efead6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.sinata.juhe;
 
import com.alipay.api.internal.util.file.IOUtils;
import com.sinata.rest.core.juhe.SecurityAESTool;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author mitao
 * @date 2024/4/1
 */
public class TelecomTest {
    //设置超时时间为5秒
    public static RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();
    // 配置您申请的KEY,在个人中心->我的数据,接口名称上方查看
    public static final String APPKEY = "";
    //明文查询地址
    public static String query_url = "https://v.juhe.cn/telecom/query?key=" + APPKEY;
    //加密查询地址
    public static String queryEncry_url = "https://v.juhe.cn/telecom/queryEncry?key=" + APPKEY;
    public static void main(String[] args) {
        // ----------------------三网手机实名制认证-----------------------------------------------------------------------
        // int queryType = 1;// 1:普通查询 2:加密查询
        // String realname = "";// 姓名
        // String idcard = "";// 身份证
        //String mobile="" // 手机号
        // int type = 1;// 是否显示手机运营商 1:显示 0:不显示(默认)
        //int showid = 1 //是否显示聚合订单账号 1:显示 0:不显示(默认)
        //int province = 1 //是否显示号码归属地 1:显示 0:不显示(默认)
        //int detail = 1 //是否买显示匹配详情码 1:显示 0:不显示(默认)
        // Map<String, Object> params = new HashMap<>();
        // params.put("realname", realname);
        // params.put("idcard", idcard);
 
        // ----------------------身份证实名查询(加密版)-----------------------------------------------------------------------
        String realname = "张三";// 姓名
        String idcard = "32072119970602561X";// 身份证
        String mobile = "18283820718";// 手机号
        String openid = "JH1ad5c53745350580f7c24ca4b09717de";// 个人中心查询
        String key = MD5(openid).substring(0, 16);//取前16位作为加密密钥
        int queryType = 2;// 加密版本
        realname = SecurityAESTool.encrypt(realname, key);//加密姓名
        idcard = SecurityAESTool.encrypt(idcard, key);//加密身份证
        mobile = SecurityAESTool.encrypt(mobile, key);//
        Map<String, Object> params = new HashMap<>();//组合参数
        params.put("realname", realname);
        params.put("idcard", idcard);
        params.put("mobile", mobile);
        //请求接口
        String result = null;
        try {
            result = queryResult(params, queryType);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //打印结果
        System.out.println(result);
 
    }
 
    /**
 
     * 请求接口查询数据
 
     * @param params 参数
     * @param type 类型,1明文查询(默认),2加密版
     * @return 结果
     * @throws Exception
     */
 
    public static String queryResult(Map<String, Object> params, int queryType) throws Exception {
 
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = null;
        String url = query_url;
        switch (queryType) {
            case 2:
                url = queryEncry_url;
                break;
        }
        try {
            url = new StringBuffer(url).append("&").append(urlencode(params)).toString();
            HttpGet httpget = new HttpGet(url);
            httpget.setConfig(config);
            response = httpClient.execute(httpget);
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                result = IOUtils.toString(resEntity.getContent(), "UTF-8");
            }
            EntityUtils.consume(resEntity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            response.close();
            httpClient.close();
        }
        return result;
    }
 
 
 
    // 将map型转为请求参数型
 
    public static String urlencode(Map<String, ?> data) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, ?> i : data.entrySet()) {
            try {
                sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        String result = sb.toString();
        result = result.substring(0, result.lastIndexOf("&"));
        return result;
    }
 
 
 
    /**
     * md5加密
     * @param data
     * @return 加密结果
     */
    public static String MD5(String data) {
        StringBuffer md5str = new StringBuffer();
        byte[] input = data.getBytes();
        try {
            // 创建一个提供信息摘要算法的对象,初始化为md5算法对象
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算后获得字节数组
            byte[] buff = md.digest(input);
            // 把数组每一字节换成16进制连成md5字符串
            int digital;
            for (int i = 0; i < buff.length; i++) {
                digital = buff[i];
                if (digital < 0) {
                    digital += 256;
                }
                if (digital < 16) {
                    md5str.append("0");
                }
                md5str.append(Integer.toHexString(digital));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md5str.toString();
    }
}