java查询手机号码归属地
2024-10-13 07:46:44
package com; 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
*/
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 JIA-G-Y
*/
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));
}else {
return (tel + "," + supplier + ","+ province + city);
}
}else {
return "无此号记录!";
}
}else{
return tel+ ":手机号码格式错误!";
}
} public static void main(String[] args) {
try {
System.out.println(getMobileLocation("13838383838"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
最新文章
- iOS第三方库管理工具
- [SDK2.2]Windows Azure Virtual Network (5) 重启、关闭、开启VNet中Virtual Machine
- Spring-程序中获取注册bean的方式
- Java计时器Timer和TimerTask用法
- iOS本地数据存取
- 转载总结 C# 多态(虚方法,抽象,接口实现)
- PowerDesigner 15 概述
- js基础小总结之string&;array&;object
- 本博客弃用,请移步http://ningios.com查看最新
- Address already in use: JVM_Bind<;null>;:8080tomcat启动不了的问题
- dom4j解析xml实例
- sql2012笔记
- IntelliJ IDEA中 todo的使用
- APP压力测试 monkey(新猿旺学习总结)
- python第五十九天-----补上笔记
- 初识numpy
- 2.17 C++类与const关键字
- 【葡萄城报表】还在为画“类Word文档报表”而发愁吗?
- POP3_使用SSL链接邮箱并获取邮件
- java中的null和";";区别------&;&;与&;的区别