java 手机号码归属地查询
下面是利用第三方接口实现手机号码归属地查询 (复制请标明出处或留言)
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;
}
}
java 手机号码归属地查询的更多相关文章
- 【原创】Java实现手机号码归属地查询
网络上已经有很多的手机号码归属地查询的API接口,但是这些接口总是有一些大大小小的缺陷. 总结一下这些缺陷: 1.要直接将它的搜索框链接形式粘到自己的页面,点击查询的时候还要跳转到他们的网站来展示归属 ...
- 本地的手机号码归属地查询-oracle数据
最近做的项目中,有个功能是手机归属地查询,因为项目要在内网下运行,所以不能用提供的webservice,只好在网上找手机归属地的数据,很多都是access的,我们的项目是用oracle,只好自己转吧, ...
- 百度手机号码归属地查询api与返回json处理
前天无意间在网上看到百度ApiStore,然后好奇就进去看了看.正好最近在某博培训Android,刚学到java基础.抱着锻炼的心态选择手机号码归属地查询api进行练手.api地址 (http://a ...
- 免费的手机号码归属地查询API接口文档
聚合数据手机号码归属四查询API接口,根据手机号码或手机号码的前7位,查询手机号码归属地信息,包括省份 .城市.区号.邮编.运营商和卡类型. 通过链接https://www.juhe.cn/docs/ ...
- PHP实现 手机号码归属地查询
在工作中需要对手机号码进行归属地查询,就在网上找了下解决方案,发现通过号段非常好判断. 我将数据库文件放到了百度网盘,请有需要的朋友去下载. 链接: https://pan.baidu.com/s/1 ...
- C# Winform实现手机号码归属地查询工具
摘要:本文介绍使用C#开发基于Winform的手机号码归属地查询工具,并提供详细的示例代码供参考. 一.需求描述 输入正确的手机号码,查询该号码的归属地和其他相关信息. 二.需求分析 1.实现手机号码 ...
- 手机号码归属地查询免费api接口代码
依据手机号码查询用户的卡类型.运营商.归属地.区域等信息. 手机归属地实体类 package org.wx.xhelper.model; /** * 手机归属地 * @author wangxw * ...
- 手机号码归属地查询api接口
淘宝网 API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON ...
- PHP手机号码归属地查询API接口
淘宝网 API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON ...
随机推荐
- sqlserver导致服务器异常卡死
1.业务反应,服务器三天两头就要重启一次,要不然直接hang掉,登上服务器,异常的慢,大概进去需要十分钟的时间,查看一下电脑配置,8核8G的物理机. 2.查看一下任务管理器中的资源使用情况,发现cpu ...
- 4、struct2的支持团队开发
在一个大型的项目中,不同的人都开发不同的模块,不能所有的人都去操作同一个struct.xml文件,我们应该对于不同的模块对应不同的配置文件 列如我们对应的登陆模块,我们可以编写一个登陆的配置文件 1. ...
- 测试同学动手搭个简易web开发项目
技术栈 node.js, vue.js, axios, python, django, orm, restful api, djangorestframework, mysql, nginx, jen ...
- ⚡ vue3 全家桶体验
前置 从创建一个简单浏览器导航首页项目展开,该篇随笔包含以下内容的简单上手: vite vue3 vuex4 vue-router next 预览效果有助于理清这些内容,限于篇幅,不容易展开叙述.由于 ...
- 车辆运动控制算法——MPC
MPC是模型预测控制算法,在车辆运动跟踪轨迹的控制中发挥很大的优势 基础的不多说,下面记录我对LQR/MPC/二次规划问题的理解 我们从LQR来引出MPC LQR的能量函数,目的是求函数J最小,即用最 ...
- 探讨NET Core数据进行3DES加密或解密弱密钥问题
前言 之前写过一篇<探讨.NET Core数据进行3DES加密和解密问题>,最近看到有人提出弱密钥问题,换个强密钥不就完了吗,猜测可能是与第三方对接导致很无奈不能更换密钥,所以产生本文解决 ...
- Promise内部实现原理
promise内部实现原理: function $Promise(fn) { // Promise 的三种状态 this.PENDING = 'pending' this.RESOLVED = 're ...
- C#利用反射实现简单记事本功能插件
效果图: 源码下载:https://github.com/doyoulaikeme/DotNetSample
- flutter gradle版本不一致
我们有时候拿到别人的项目时,直接运行可能会报错,因为gradle版本不一样,此时需要修改这两个地方: 要把这两个版本改成你本地的版本就可以了,怎么看呢,一般你自己创建过flutter项目,里面也有这个 ...
- 谈谈你对 TCP 三次握手和四次挥手的理解
TCP三次握手: 1.客户端发送syn包到服务器,等待服务器确认接收. 2.服务器确认接收syn包并确认客户的syn,并发送回来一个syn+ack的包给客户端. 3.客户端确认接收服务器的syn+ac ...