根据手机号获取手机号归属地
前言
我们在做开发的时候,可能会遇到根据手机号反推出归属地的问题,所以就找了写资料,反推只能精确到省份。
/**
* 获取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 = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + tel;
String result = callUrlByGet(url, "GBK").replace("__GetZoneResult_ =", "");
HashMap hashMap = JSON.parseObject(result, HashMap.class);
String province = hashMap.get("province").toString();
if ("".equals(province)){
return "无此号记录!";
}else {
return province;
}
}
return "无此号记录!";
}
用的时候直接调用即可。

浙公网安备 33010602011771号