下面是利用第三方接口实现手机号码归属地查询 (复制请标明出处或留言)
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; } }
原文地址:https://www.cnblogs.com/yuyuchen/p/10417220.html
时间: 2024-10-17 06:15:09