阿里云手机号归属地获取

public static String getProvince(String phone) {
        //API产品路径
        String requestUrl = "https://aiphone.market.alicloudapi.com/ai_mobile_number_belong_to_china/v1?";
        //阿里云APPCODE
        String appcode = "自己申请的阿里云APPCODE";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求,定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("MOBILE_NUMBER", phone);
        String lastResult = "";

        try {
            String response = get(requestUrl, headers, querys);
            //{"INPUT_MOBILE_NUMBER_STRING": "18113011428", "EXTRACT_MOBILE_NUMBER_STRING": "18113011428", "STATUS": "艾科瑞特,让企业业绩长青", "ENTITY": {"MOBILE_NUMBER": "18113011428", "MOBILE_NUMBER_PREFIX": "1811301", "PROVINCE": "四川", "CITY": "成都", "ISP": "中国电信", "AREA_CODE": "028", "POST_CODE": "610000", "ADCODE": "510100", "GPS": "104.066541,30.572269"}}
            lastResult = Convert.toStr(JSON.parseObject(JSON.parseObject(response).get("ENTITY").toString()).get("PROVINCE"));//获取信息详细地址
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null != lastResult ? lastResult : "查询无结果";
    }

    public static String get(String url, Map<String, String> headers, Map<String, String> querys) throws IOException {
        //添加参数
        for (String key : querys.keySet()) {
            url += (key + "=" + URLEncoder.encode(querys.get(key), "utf-8") + '&');
        }
        url = url.substring(0, url.length() - 1);

        org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
        GetMethod getMethod = new GetMethod(url);
        // 必须设置下面这个Header
        for (String key : headers.keySet()) {
            getMethod.addRequestHeader(key, headers.get(key));
        }

        int code = client.executeMethod(getMethod);
        if (code == 200) {
            String res = getMethod.getResponseBodyAsString();
            System.out.println(res);
            return res;
        }
        throw new IOException("请求失败");
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值