java 手机号码归属地查询

本文介绍了一种利用第三方API实现手机号码归属地查询的方法,包括通过淘宝、k780及IP138网站获取号码详细信息的Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     下面是利用第三方接口实现手机号码归属地查询 (复制请标明出处或留言)

 

package com.test.yyc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class PhoneNumberBelong {
    public static void main(String[] args) {
        String mobileNumber = "13333333333";
        try {
            //System.out.println(calcMobileCity(mobileNumber));
            //System.out.println(queryMobileLocation(mobileNumber));
            System.out.println(queryMobileLocationk780(mobileNumber)); //{address=中国,河北,秦皇岛, area_code=0335}           
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static String calcMobileCity(String mobileNumber)
            throws MalformedURLException {
        String result = "";
        try {
            String urlString = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="
                    + mobileNumber;
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();
            connection.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "GBK"));

            String line;
            while ((line = reader.readLine()) != null) {
                result = result + line;
                result = result + "\n";
            }
            reader.close();
            if (!(StringUtils.isEmpty(result))) {
                Pattern p = Pattern.compile("province:'([^',]*)");
                Matcher m = p.matcher(result);
                while (m.find()) {
                    result = m.group(1);
                }
                connection = null;
                return result;
            }
            return "无此号记录!";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 使用k780公司的接口
     * @param tel
     * @return
     * @throws Exception
     */
    public static Map<String, String> queryMobileLocationk780(String tel) throws Exception {
        Pattern pattern = Pattern.compile("1\\d{10}");
        Matcher matcher = pattern.matcher(tel);
        Map<String, String> resultMap = new HashMap<String, String>();
        String address = "";
        String areaCode = "";
        if (matcher.matches()) {
            String url = "http://api.k780.com:88/?app=phone.get&phone=" + tel +"&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
            String result = callUrlByGet(url, "UTF-8");
            if (!(StringUtils.isEmpty(result))) {
                JSONObject json = new JSONObject(result);
                if(json.getString("success").equals("1")){ // 请求成功
                    JSONObject resultJson = json.getJSONObject("result");
                    if(resultJson.getString("status").indexOf("NOT") <= -1){
                        address = resultJson.getString("style_simcall");
                        areaCode = resultJson.getString("area");
                    } else {
                        address = "未知归属地";
                    }
                } else { // 请求失败
                    address = "未知归属地";
                    areaCode = "";
                }
            } else {
                address = "未知归属地";
                areaCode = "";
            }

            resultMap.put("address", address);
            resultMap.put("area_code", areaCode);
            return resultMap;
        }

        return resultMap;
    }
    private static String callUrlByGet(String callurl, String charset) {
        String result = "";
        try {
            URL url = new URL(callurl);
            URLConnection connection = url.openConnection();
            connection.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), charset));

            String line;
            while ((line = reader.readLine()) != null) {
                result = result + line;
                result = result + "\n";
            }
            reader.close();
            connection = null;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return result;
    }
    /**
     * 通过解析 IP138网站的html代码来获取号码归属地信息
     * @param mobile
     * @return
     */
    public static Map<String, String> queryMobileLocation(String mobile){
        String url = "http://www.ip138.com:8080/search.asp?action=mobile&mobile="+mobile;
        Map<String, String> resultMap = new HashMap<String, String>();
        try {
            Document doc = Jsoup.connect(url).get();
            try {
                Elements els = doc.getElementsByClass("tdc2");
                String address = els.get(1).text();
                String areaCode = els.get(3).text();
                String corp = els.get(2).text();
                String postCode = els.get(4).text();
                if(postCode != null && !"".equals(postCode)){
                    postCode = postCode.substring(0, 6);
                }
                
                String[] addresss = address.split(" ");
                String city = "";
                String province = "";
                if(addresss.length > 0){
                    province = addresss[0];
                    if(addresss.length > 1){
                        city = addresss[1];
                    } else {
                        city = "";
                    }
                } else {
                    province = "";
                    city = "";
                }
                resultMap.put("province", province);
                resultMap.put("city", city);
                resultMap.put("areaCode", areaCode);
                resultMap.put("corp", corp);
                resultMap.put("postCode", postCode);
            } catch (Exception e) {
                e.printStackTrace();
                resultMap.put("province", "");
                resultMap.put("city", "");
                resultMap.put("areaCode", "");
                resultMap.put("corp", "");
                resultMap.put("postCode", "");
            }
        } catch (IOException e) {
            e.printStackTrace();
            resultMap.put("province", "");
            resultMap.put("city", "");
            resultMap.put("areaCode", "");
            resultMap.put("corp", "");
            resultMap.put("postCode", "");
        }
        
        return resultMap;
    }
}

 

转载于:https://www.cnblogs.com/yuyuchen/p/10417220.html

适配机型/应用平台 Java/S40 适用机型:诺基亚 7610S; 7510a; 7310c; 7070; 6103; 5000; 3120c; 2680s; 2600c; 1680c; 5220xm; 3600s; 7360; 7380; 6070; 8800 SE; 5300; 6088; 8800 Sirocco; 6086; 8600 Luna; 3500c; 7900; 6131i; 6263; 7500; 8800 Arte; 8600; 7390; 7373; 7370; 6555; 6288; 6280; 6275; 6267; 6165; 6151; 6136; 6133; 6131; 6126; 6125; 6111; 6085; 6080; 6060; 6021; 5070; 3500; 3110; 3109; 2760; 2630; 2626; 2610; 6301; 5610xm; 5310xm; 6108; 7260; 6270; 6030; 6020; 3220; 3120; 2865; 6300; 6234; 6233; 5200; 6500c; 6500s; 7600; 7210; 7200; 6800; 6650; 6610; 6230; 6220; 6100; 5100; 3300; 3200; 3100 摩托罗拉 E398; EM25; EM28; K3; KRZR; L6; L6g; L800t; Pebl U3; RAZR maxx V1100; ROKR W5; SLVR L6i; SLVR L7; SLVR L71; SLVR L72; V3; V300; V303; V500; V501; V600; VE75; W206; W230; W270; W360; W380; W388; W395; W396; W510; W6; ZN200; w231 索尼爱立信 C702; C902; G502; K660i; R306c; T280i; T303c; W350c; W760c; Z550c; Z558c; Z600; Z608; Z610i; Z780 三星 D528; D808; D828; D838; D848; D888; D908; D908i; D988; E218; E258; E428; E598; E848; E878; E898; E908; E958; F218; F258; F268; F278; F308; F488E; G608; G618; G800; G808E; J608; J618; J708; M318; M618; M628; Soulb; U308; U600; U608; U708E; U800; U808E; U900; U908E; W579; W629; X838 联想 E520; P705; P719; P790; P960; P990; S320; S530; S6; S600; S7; S70; S9; V80; i510; i758; i760; i817; i827; i880; i906; i909; i966; i968; p609; s90; s96 夏新 A510; A530; A616; A636; E3; E5; M515; M690 天语 A615; A902; A905; A906; A908; A909; A930; B922; C280; C800; D705; E60
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值