【原创】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的字符串
具体实现:
  
 1 /**
 2 * @ClassName: HttpClientUtil 
 3 * @Description: html页面抓取素有字符串工具类
 4 * @author: chenkaideng
 5 * @date 2015年11月2日 下午3:55:49 
 6  */
 7 
 8 import java.io.BufferedReader;
 9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.net.HttpURLConnection;
12 import java.net.URL;
13 import org.apache.commons.io.IOUtils;
14 import org.apache.commons.lang3.StringUtils;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 
18 
19 public class HttpClientUtil {
20     private static final Logger logger = LoggerFactory.getLogger("HttpClient");
21     private String readInputStream(InputStream instream, String charest) throws Exception {
22         StringBuilder sb = new StringBuilder();
23         try(
24                 InputStreamReader isr = new InputStreamReader(instream, charest);
25                 BufferedReader reader = new BufferedReader(isr);) {
26             String line = null;
27             while ((line = reader.readLine()) != null) {
28                 sb.append(line);
29             }
30         }
31         return sb.toString();
32         
33     } 
34     
35     public String getWebcontent(String webUrl, String charest) {
36         if (StringUtils.isEmpty(webUrl)) 
37             return null;
38         int response = -1;
39         HttpURLConnection conn = null;
40         try {            
41             URL url = new URL(webUrl);
42             conn = (HttpURLConnection) url.openConnection();
43             conn.setRequestMethod("GET");
44             conn.setReadTimeout(60 * 2000);
45             conn.setConnectTimeout(10 * 1000);
46             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");
47             conn.setDoOutput(true);
48             conn.connect();
49             response = conn.getResponseCode();
50             if (response == 200) {
51                 InputStream im = null;                
52                 try {
53                     im = conn.getInputStream();
54                     return readInputStream(im, charest);
55                 } finally {
56                     IOUtils.closeQuietly(im);
57                 }
58             }
59             return null;
60         } catch (Exception e) {
61             logger.error(String.format("下载到文件出错[url=%s][%s][responsecode=%d]", webUrl, e.getMessage(), response));
62             return null;
63         } finally {
64             if(conn != null) {
65                 conn.disconnect();
66                 conn = null;
67             }
68         } 
69     }
70 }

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

 1 import com.alibaba.fastjson.JSONObject;
 2 /**
 3  * 
 4 * @ClassName: GetMobileMessage
 5 * @Description: TODO
 6 * @author chenkaideng@star-net.cn
 7 * @date 2016年1月28日 下午2:40:56
 8 *
 9  */
10 public class GetMobileMessage{
11     private static final String PHONE_PLACE_API_URL="http://virtual.paipai.com/extinfo/GetMobileProductInfo";
12     /**
13      * 
14     * @Title: getMobilePlace 
15     * @Description: 获取手机归属地信息
16     * @param @param mobile
17     * @param @return     
18     * @return String     
19     * @throws
20      */
21     public String getMobilePlace(String mobile){
22         HttpClientUtil util = new HttpClientUtil();
23         String[] strings={"",""};
24         try {
25             //访问拍拍的查询接口
26             String mobileMessage = util.getWebcontent(PHONE_PLACE_API_URL+"?mobile="+mobile+"&amount=10000", "GB2312");
27             strings = mobileMessage.split(";");
28             //(页面获取到的消息,除了这些,还有一些html语句)
29 //            string[0]="({mobile:'15850781443',province:'江苏',isp:'中国移动',stock:'1',amount:'10000',maxprice:'0',minprice:'0',cityname:'南京'})";
30             mobileMessage = strings[0];
31             JSONObject jsonObject = JSONObject.parseObject(mobileMessage.substring(1, mobileMessage.length()-1));
32             //解析出省份和city和运营商
33             String province = jsonObject.getString("province");
34             String cityname = jsonObject.getString("cityname");
35             String isp = jsonObject.getString("isp");
36             return isp+"&nbsp"+province+cityname;
37         } catch (Exception e) {
38             e.printStackTrace();
39 //            logger.error(strings[0]+e.toString());
40             return "";
41         } 
42     }
43 }

 

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

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

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

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

 
posted @ 2016-01-28 14:53  马叉虫  阅读(13348)  评论(1编辑  收藏  举报