Java 查詢手機(jī)號(hào)歸屬地 基于淘寶Api,百度Api

代碼基于hutool工具包 和 lombok制作

主要代碼是用hutool提供http工具類和json工具類進(jìn)行完成

<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.9</version>
</dependency>
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class PhoneEntity {

    private String province;

    private String city;

    // 如 中國(guó)聯(lián)通...
    private String type;

}
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.szxcharge.cloud.user.common.entity.PhoneEntity;
import lombok.extern.slf4j.Slf4j;

/**
 * 手機(jī)號(hào)查詢省份 運(yùn)營(yíng)商信息
 * @author orange
 * @email 1067357662@qq.com
 */
@Slf4j
public class PhoneQueryUtil {

    /**
     * 查詢手機(jī)號(hào)信息
     * @return null=信息查詢不到
     */
    public static PhoneEntity query(String phoneNumber) {
        PhoneEntity phoneEntity = queryByBaiduApi(phoneNumber);
        if (null == phoneEntity) {
            phoneEntity = queryByTaobaoApi(phoneNumber);
        }
        return phoneEntity;
    }

    /**
     * 通過(guò)淘寶接口查詢手機(jī)號(hào)信息
     */
    private static PhoneEntity queryByTaobaoApi(String phoneNumber) {
        try {
            String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + phoneNumber;
            HttpResponse response = HttpRequest.get(url).execute();
            if (response.getStatus() != HttpStatus.HTTP_OK) return null;

            String body = response.body().replace("__GetZoneResult_ = ", "");
            if (StrUtil.isBlank(body)) return null;

            JSONObject parseObj = JSONUtil.parseObj(body);
            String province = parseObj.getStr("province");
            String type = parseObj.getStr("catName");

            if (StrUtil.isBlank(province) || StrUtil.isBlank(type)) return null;

            PhoneEntity phoneEntity = new PhoneEntity();
            phoneEntity.setProvince(province);
            phoneEntity.setType(type);

            return phoneEntity;
        } catch (Exception e) {
            log.error("手機(jī)號(hào)查詢信息出錯(cuò): {}", ExceptionUtil.stacktraceToString(e));
            return null;
        }
    }

    /**
     * 通過(guò)百度接口查詢手機(jī)號(hào)信息
     */
    private static PhoneEntity queryByBaiduApi(String phoneNumber) {
        try {
            String url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?resource_name=guishudi&query=" + phoneNumber;
            HttpResponse response = HttpRequest.get(url).execute();
            if (response.getStatus() != HttpStatus.HTTP_OK) return null;

            String body = response.body();
            if (StrUtil.isBlank(body)) return null;

            JSONObject parseObj = JSONUtil.parseObj(body);
            JSONArray parseObjJSONArray = parseObj.getJSONArray("data");
            if (null == parseObjJSONArray || parseObjJSONArray.size() == 0) return null;

            JSONObject dataObject = parseObjJSONArray.getJSONObject(0);

            String province = dataObject.getStr("prov");
            String type = dataObject.getStr("type");
            String city = dataObject.getStr("city");

            if (StrUtil.isBlank(province) || StrUtil.isBlank(type)) return null;

            PhoneEntity phoneEntity = new PhoneEntity();
            phoneEntity.setProvince(province);
            phoneEntity.setType(type);
            phoneEntity.setCity(city);

            return phoneEntity;
        } catch (Exception e) {
            log.error("手機(jī)號(hào)查詢信息出錯(cuò): {}", ExceptionUtil.stacktraceToString(e));
            return null;
        }
    }

    public static void main(String[] args) {
        PhoneEntity phoneEntity = PhoneQueryUtil.query("xxxxxxxxx");
        if (null != phoneEntity) {
            System.out.println(phoneEntity.getProvince());
            System.out.println(phoneEntity.getType());
            System.out.println(phoneEntity.getCity());
        }
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容