New file |
| | |
| | | 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(); |
| | | } |
| | | } |