網絡上已經有不少的手機號碼歸屬地查詢的API接口,可是這些接口老是有一些大大小小的缺陷。php
總結一下這些缺陷:html
一、要直接將它的搜索框連接形式粘到本身的頁面,點擊查詢的時候還要跳轉到他們的網站來展現歸屬地結果java
二、提供接口的API,通常都要求付費,或者一天只有免費的限定查詢次數web
三、有些博客文檔中的API已通過於老舊,嘗試的時候,已經404Not Found的了apache
因此寫篇博客,供正在作手機歸屬地查詢的小夥伴參考。json
思路:網絡
->我找到一個拍拍網的接口,能夠經過curl直接傳手機號碼來進行查詢,而且會返回給咱們一個相似json的字符串(其實不是Json,就是一些字符串裏面有咱們想要的信息)app
->java經過HttpURLConnection去鏈接這個地址,而且抓取到所返回頁面的全部字符串,這些字符串中就含有上述的類json的結果curl
->那咱們拿到這個字符串,解析出咱們想要的通信商和省份城市等信息就能夠了ide
說明:
拍拍網查手機歸屬地地址: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+" "+province+cityname; 37 } catch (Exception e) { 38 e.printStackTrace(); 39 // logger.error(strings[0]+e.toString()); 40 return ""; 41 } 42 } 43 }
這樣就能夠免費獲得手機號的歸屬地信息了,並且能夠做爲本身的一個工具方法使用,你們愛怎麼封裝就怎麼封裝,
否則查個歸屬地還要收費還要給別人網站作廣告,實屬不爽啊。
可是惟一的缺陷就是,拍拍要是把這個地址一改,就得跟着改咯。
不過不要緊,都給整這個思路,什麼地址什麼接口都能整出歸屬地。