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

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 手机号码归属地查询的更多相关文章

  1. 【原创】Java实现手机号码归属地查询

    网络上已经有很多的手机号码归属地查询的API接口,但是这些接口总是有一些大大小小的缺陷. 总结一下这些缺陷: 1.要直接将它的搜索框链接形式粘到自己的页面,点击查询的时候还要跳转到他们的网站来展示归属 ...

  2. 本地的手机号码归属地查询-oracle数据

    最近做的项目中,有个功能是手机归属地查询,因为项目要在内网下运行,所以不能用提供的webservice,只好在网上找手机归属地的数据,很多都是access的,我们的项目是用oracle,只好自己转吧, ...

  3. 百度手机号码归属地查询api与返回json处理

    前天无意间在网上看到百度ApiStore,然后好奇就进去看了看.正好最近在某博培训Android,刚学到java基础.抱着锻炼的心态选择手机号码归属地查询api进行练手.api地址 (http://a ...

  4. 免费的手机号码归属地查询API接口文档

    聚合数据手机号码归属四查询API接口,根据手机号码或手机号码的前7位,查询手机号码归属地信息,包括省份 .城市.区号.邮编.运营商和卡类型. 通过链接https://www.juhe.cn/docs/ ...

  5. PHP实现 手机号码归属地查询

    在工作中需要对手机号码进行归属地查询,就在网上找了下解决方案,发现通过号段非常好判断. 我将数据库文件放到了百度网盘,请有需要的朋友去下载. 链接: https://pan.baidu.com/s/1 ...

  6. C# Winform实现手机号码归属地查询工具

    摘要:本文介绍使用C#开发基于Winform的手机号码归属地查询工具,并提供详细的示例代码供参考. 一.需求描述 输入正确的手机号码,查询该号码的归属地和其他相关信息. 二.需求分析 1.实现手机号码 ...

  7. 手机号码归属地查询免费api接口代码

    依据手机号码查询用户的卡类型.运营商.归属地.区域等信息. 手机归属地实体类 package org.wx.xhelper.model; /** * 手机归属地 * @author wangxw * ...

  8. 手机号码归属地查询api接口

    淘宝网 API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON ...

  9. PHP手机号码归属地查询API接口

    淘宝网 API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON ...

随机推荐

  1. 【SpringMVC】

    前言

  2. cbitmap 获取RGB

    CBitMap的用法   MFC提供了位图处理的基础类CBitmap,可以完成位图(bmp图像)的创建.图像数据的获取等功能.虽然功能比较少,但是在对位图进行一些简单的处理时,CBitmap类还是可以 ...

  3. Nginx 从入门到放弃(二)

    学习完了nginx的基本知识后,我们来了解下nginx的虚拟主机. 说到虚拟主机,那就得说一说虚拟主机的三种方式了 基于端口的虚拟主机 基于域名的虚拟主机 基于ip的虚拟主机 基于端口实现虚拟主机 只 ...

  4. idea 链接 Tomcat 出现的错误 Application server libraries not found

    红色字体是重点 好久好久没有上来了.一直忙于工作没事有时间进行总结. 最近在家隔离期间发现了一个好玩的游戏率土之滨  , 玩的不错和大盟主混的很好.在给盟里做考勤任务的时候发现很麻烦还需要用Excel ...

  5. 使用SpringBoot构建REST服务-什么是REST服务

    前言: 本文按照Spring官网构建REST服务的步骤测试,可以得到结论: 到底什么样的风格才是RESTful风格呢? 1,约束请求命令如下: GET,获取资源.例如:/employees表示获取列表 ...

  6. 记一次开发CefSharp做浏览器时关闭页面上时未释放遇到的小问题

    问题:当CefSharp放在List里,然后用了Remove移除,CefSharp是否还存在. 我将Cefsharp做成UserControl控件,然后在Main页面里采用List<UserCo ...

  7. 火车运输(最大生成树+lca) 洛谷P1967

    货车运输 题目描述 \(A\) 国有 \(n\) 座城市,编号从 \(1\) 到 \(n\) ,城市之间有 \(m\) 条双向道路.每一条道路对车辆都有重量限制,简称限重. 现在有 \(q\) 辆货车 ...

  8. 「疫期集训day6」雨林

    是的,他们击退了我们,那又怎样,他们饥肠辘辘,弹尽粮绝...----阿尔贡森林中的士兵 今天考试一般,感觉难度比第一次考试要大的多,T2板子整合(元宵节原题,然而那次考试我都没参加),T1搜索,T3有 ...

  9. 「区间DP」「洛谷P1043」数字游戏

    「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...

  10. 如何排查CPU占用太高

    线上项目运行时,出现问题不像在本地那么容易排查,经常需要借助日志.或者一些工具来找出问题.cpu被占满我们经常会遇到.比如我们有这样一段代码: public Class Demo1_16 { publ ...