手机号获取归属地 获取到到城市
2016-05-03 16:47
281 查看
package com.xune.mr.mgmr.common.utils.mobileaddr; 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 { /** * 获取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(); // return (tel + "," + supplier + ","+ province + city); // return (province + city + "," + supplier ); if (province.equals(city)) { return province; } return (province + city); } else { return "无此号记录!"; } } else { return tel + ":手机号码格式错误!"; } } public static void main(String[] args) { try { System.out.println(MobileLocationUtil .getMobileLocation("18233583311")); } catch (Exception e) { e.printStackTrace(); } } }
相关文章推荐
- css预处理器--less
- Ext.each的用法
- 转:SPI、I2C、UART、USART串行总线协议的区别
- Extjs6中BufferedStore组件
- 实现点击超链接<a>标签,点击的标签变色,标注
- SIFT-FCACO算法的图像配准
- hive与hbase的集成
- 【转:POI操作Excel详解,HSSF和XSSF两种方式】
- 绕过WAF的SQL注入语句
- MySQL调优
- jQuery中队列queue()函数的实例教程
- 信息流广告投放入门
- window下erlang(18.3)与rabbitmq(3.6.1)安装
- 关于二叉树的前序、中序、后序问题
- 图像处理中,SIFT,FAST,MSER,STAR等特征提取算法的比较与分析(利用openCV实现)
- Java 集合之Collection 接口和遍历方法
- solrStudy-中文分词器
- MATLAB基础
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
- Windows 安装配置 Zabbix Agentd