【原创】Java实现手机号码归属地查询

时间:2023-03-08 15:04:34
【原创】Java实现手机号码归属地查询

网络上已经有很多的手机号码归属地查询的API接口,但是这些接口总是有一些大大小小的缺陷。

总结一下这些缺陷:

1、要直接将它的搜索框链接形式粘到自己的页面,点击查询的时候还要跳转到他们的网站来展示归属地结果

2、提供接口的API,一般都要求付费,或者一天只有免费的限定查询次数

3、有些博客文档中的API已经过于老旧,尝试的时候,已经404Not Found的了

所以写篇博客,供正在做手机归属地查询的小伙伴参考。

思路:

  ->我找到一个拍拍网的接口,可以通过curl直接传手机号码来进行查询,并且会返回给我们一个类似json的字符串(其实不是Json,就是一些字符串里面有我们想要的信息)

  ->java通过HttpURLConnection去连接这个地址,并且抓取到所返回页面的所有字符串,这些字符串中就含有上述的类json的结果

  ->那我们拿到这个字符串,解析出我们想要的通讯商和省份城市等信息就可以了

说明:

  拍拍网查手机归属地地址:http://virtual.paipai.com/extinfo/GetMobileProductInfo?mobile=15850781443&amount=10000

  参数说明:mobile:手机号码

       amount:未知(但是必须要有,不然查询不出结果)
  返回值:类似JSON的字符串
具体实现:
  
 /**
* @ClassName: HttpClientUtil
* @Description: html页面抓取素有字符串工具类
* @author: chenkaideng
* @date 2015年11月2日 下午3:55:49
*/ import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class HttpClientUtil {
private static final Logger logger = LoggerFactory.getLogger("HttpClient");
private String readInputStream(InputStream instream, String charest) throws Exception {
StringBuilder sb = new StringBuilder();
try(
InputStreamReader isr = new InputStreamReader(instream, charest);
BufferedReader reader = new BufferedReader(isr);) {
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}
return sb.toString(); } public String getWebcontent(String webUrl, String charest) {
if (StringUtils.isEmpty(webUrl))
return null;
int response = -1;
HttpURLConnection conn = null;
try {
URL url = new URL(webUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(60 * 2000);
conn.setConnectTimeout(10 * 1000);
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
conn.setDoOutput(true);
conn.connect();
response = conn.getResponseCode();
if (response == 200) {
InputStream im = null;
try {
im = conn.getInputStream();
return readInputStream(im, charest);
} finally {
IOUtils.closeQuietly(im);
}
}
return null;
} catch (Exception e) {
logger.error(String.format("下载到文件出错[url=%s][%s][responsecode=%d]", webUrl, e.getMessage(), response));
return null;
} finally {
if(conn != null) {
conn.disconnect();
conn = null;
}
}
}
}

  然后调用上述的工具类,带着手机号码参数去访问拍拍的接口地址,抓到页面,解析出归属地信息就可以了

 import com.alibaba.fastjson.JSONObject;
/**
*
* @ClassName: GetMobileMessage
* @Description: TODO
* @author chenkaideng@star-net.cn
* @date 2016年1月28日 下午2:40:56
*
*/
public class GetMobileMessage{
private static final String PHONE_PLACE_API_URL="http://virtual.paipai.com/extinfo/GetMobileProductInfo";
/**
*
* @Title: getMobilePlace
* @Description: 获取手机归属地信息
* @param @param mobile
* @param @return
* @return String
* @throws
*/
public String getMobilePlace(String mobile){
HttpClientUtil util = new HttpClientUtil();
String[] strings={"",""};
try {
//访问拍拍的查询接口
String mobileMessage = util.getWebcontent(PHONE_PLACE_API_URL+"?mobile="+mobile+"&amount=10000", "GB2312");
strings = mobileMessage.split(";");
//(页面获取到的消息,除了这些,还有一些html语句)
// string[0]="({mobile:'15850781443',province:'江苏',isp:'中国移动',stock:'1',amount:'10000',maxprice:'0',minprice:'0',cityname:'南京'})";
mobileMessage = strings[0];
JSONObject jsonObject = JSONObject.parseObject(mobileMessage.substring(1, mobileMessage.length()-1));
//解析出省份和city和运营商
String province = jsonObject.getString("province");
String cityname = jsonObject.getString("cityname");
String isp = jsonObject.getString("isp");
return isp+"&nbsp"+province+cityname;
} catch (Exception e) {
e.printStackTrace();
// logger.error(strings[0]+e.toString());
return "";
}
}
}

这样就可以免费得到手机号的归属地信息了,而且可以作为自己的一个工具方法使用,大家爱怎么封装就怎么封装,

不然查个归属地还要收费还要给别人网站做广告,实属不爽啊。

但是唯一的缺陷就是,拍拍要是把这个地址一改,就得跟着改咯。

不过没关系,都给整这个思路,什么地址什么接口都能整出归属地。