java获取手机号归属地
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException; public class MobileLocationUtil {
/**
* 归属地查询
* @param mobile
* @return mobileAddress
*/
@SuppressWarnings("unused")
private static String getLocationByMobile(final String mobile) throws ParserConfigurationException, SAXException, IOException{
String MOBILEURL = " http://www.youdao.com/smartresult-xml/search.s?type=mobile&q=";
String result = callUrlByGet(MOBILEURL + mobile, "GBK");
StringReader stringReader = new StringReader(result);
InputSource inputSource = new InputSource(stringReader);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(inputSource); if (!(document.getElementsByTagName("location").item(0) == null)) {
return document.getElementsByTagName("location").item(0).getFirstChild().getNodeValue();
}else{
return "无此号记录!";
}
}
/**
* 获取URL返回的字符串
* @param callurl
* @param charset
* @return
*/
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 += line;
result += "\n";
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
return result;
}
/**
* 手机号码归属地
* @param tel 手机号码
* @return 135XXXXXXXX,联通/移动/电信,湖北武汉
* @throws Exception
* @author
*/
public static String getMobileLocation(String tel) throws Exception{
Pattern pattern = Pattern.compile("1\\d{10}");
Matcher matcher = pattern.matcher(tel);
if(matcher.matches()){
String url = "http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=" + tel;
String result = callUrlByGet(url,"GBK");
StringReader stringReader = new StringReader(result);
InputSource inputSource = new InputSource(stringReader);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(inputSource);
String retmsg = document.getElementsByTagName("retmsg").item(0).getFirstChild().getNodeValue();
if(retmsg.equals("OK")){
String supplier = document.getElementsByTagName("supplier").item(0).getFirstChild().getNodeValue().trim();
String province = document.getElementsByTagName("province").item(0).getFirstChild().getNodeValue().trim();
String city = document.getElementsByTagName("city").item(0).getFirstChild().getNodeValue().trim();
if (province.equals("-") || city.equals("-")) { // return (tel + "," + supplier + ","+ getLocationByMobile(tel));
return (getLocationByMobile(tel) + "," + supplier);
}else { // return (tel + "," + supplier + ","+ province + city);
return (province + city + "," + supplier );
} }else { return "无此号记录!"; } }else{ return tel+ ":手机号码格式错误!"; } } public static void main(String[] args) {
try {
System.out.println(MobileLocationUtil.getMobileLocation("187****254"));
} catch (Exception e) {
e.printStackTrace();
}
} }
java技术交流群:57388149
java获取手机号归属地的更多相关文章
- 使用java获取手机号归属地等信息httpClient实现
java获取手机号归属地 一般想获取手机号归属地等信息个人是无法获取的,但是可以通过调用第三方接口获取,具体百度搜索很多这里例子提供一个淘宝的接口 ,该功能已经发布到网站作为一个在线小工具,拿走不谢: ...
- 手把手教你用Java获取IP归属地
前几个月微信公众号上线了IP归属地的功能,后续知乎.抖音等平台纷纷添加了该功能.如果是国内的用户精确到省份,国外用户精确到国家.本文就使用Java实现获取IP归属地. ! 主要讲解几个步骤: Java ...
- java 获取手机归属地,引起net.UnknownHostException错误
这个问题是请求,重定向了,跟入源码.修改了地址,变成302 Connection connect = Jsoup.connect(url); connect.header("Host&quo ...
- android手机获取手机号
最近做到一个项目,需要获取手机号.项目中使用的手机卡为电信卡,那么就以此为例吧. 网上不符合需求的方法 Google了一下,网上的做法如下: //获取手机号码 TelephonyManager tm ...
- Android 开发工具类 31_WebService 获取手机号码归属地
AndroidInteractWithWebService.xml <?xml version="1.0" encoding="utf-8"?> & ...
- uni-app开发经验分享二十: 微信小程序 授权登录 获取详细信息 获取手机号
授权页面 因为微信小程序提供的 权限弹窗 只能通用户确认授权 所以可以 写一个授权页面,让用户点击 来获取用户相关信息 然后再配合后台就可以完成登录 <button class="bt ...
- java获取https网站证书,附带调用https:webservice接口
一.java 获取https网站证书: 1.创建一个java工程,新建InstallCert类,将以下代码复制进去 package com; import java.io.BufferedReader ...
- java获取图片原始尺寸
java获取图片原始尺寸 URL url = null; InputStream is = null; BufferedImage img = null; try { url = new URL(pi ...
- java学习第13天( java获取当前时间,有关大数据的运算及精确数字运算,Date类)
一 java获取当前时间 学习一个函数,得到当前时间的准确值 System.currectTimeMillis(). 可以得到以毫秒为单位的当前时间.它主要用于计算程序运行时间,long start= ...
随机推荐
- Todd's Matlab讲义第6讲:割线法
割线法 割线法求解方程\(f(x)=0\)的根需要两个接近真实根\(x^\*\)的初值\(x_0\)和\(x_1\),于是得到函数\(f(x)\)上两个点\((x_0,y_0=f(x_0))\)和\( ...
- MongoDB副本集学习(三):性能和优化相关
Read Preferences/读写分离 有时候为了考虑应用程序的性能或响应性,为了提高读取操作的吞吐率,一个常见的措施就是进行读写分离,MongoDB副本集对读写分离的支持是通过Read Pref ...
- 【PHP面向对象(OOP)编程入门教程】6.如何去使用对象中的成员
上面看到PHP对象中的成员有两种一种是成员属性, 一种是成员方法.对象我们以经可以声明了,$p1=new Person(); 怎么去使用对象的成员呢?要想访问对象中的成员就要使用一个特殊的操作符”-& ...
- 虚拟机安装Ubuntu三种网络模式
VMWare提供三种工作模式桥接(bridge).NAT(网络地址转换)和host-only(主机模式). NAT(网络地址转换) 在NAT模式下,虚拟系统需要借助NAT(网络地址转换)功能,通过宿主 ...
- linux安装包地址备忘
64位系统安装包: http://mirrors.163.com/centos/5/os/x86_64/CentOS/ 32位系统安装包: http://mirrors.163.com/centos/ ...
- HDU 1533 最小费用最大流(模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void ...
- 数据结构大二课程设计:QT实现线段树
源码以及编译文件下载地址:http://download.csdn.net/detail/zhiyanpianyu1234/9445909#comment 加入了一些小东西,一直觉得课设是做给自己看的 ...
- COGS 2479 偏序 题解
[题意] 给定一个有n个元素的序列,元素编号为1~n,每个元素有三个属性a,b,c,求序列中满足i<j且ai<aj且bi<bj且ci<cj的数对(i,j)的个数. 对于30%的 ...
- 数论v2
#include <cmath> #include <cstdio> #include <cstring> #include <algorithm> # ...
- 【MySQL】MySQL 如何实现 唯一随机数ID
如果不是 UUID 好像比较困难 参考资料: http://bbs.csdn.net/topics/390001507 https://www.zhihu.com/question/20151242