天天看點

使用java擷取手機号歸屬地等資訊httpClient實作

java擷取手機号歸屬地

一般想擷取手機号歸屬地等資訊個人是無法擷取的,但是可以通過調用第三方接口擷取,具體百度搜尋很多

這裡例子提供一個百度的接口 ,該功能已經釋出到網站作為一個線上小工具,

拿走不謝:www.yzcopen.com/con/iphone

用都的jar httpClient相關

代碼:

public class HttpTool {

/**
 * 手機号地區接口
 * @param request
 * @return
 */
private final static String iphoneurl = "http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel=";

/**
 * http請求
 * @return
 * @throws Exception
 */
public static String doHttpgetAPi(String url)  throws Exception {
    HttpGet post = null;
     CloseableHttpResponse response = null;
    try {
        CloseableHttpClient httpClient = HttpClients.createDefault();
         post = new HttpGet(url);
        // 構造消息頭
         post.setHeader("Content-Type", "application/json; charset=utf-8");
         post.setHeader("Connection", "Close");
         response =  httpClient.execute(post);
        if(response != null){
              HttpEntity entity=response.getEntity();  
                String str=EntityUtils.toString(entity); 
              return str;
        }
        return null;
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        if(response!=null){
            response.getEntity().getContent().close();
        }
        if(post != null){
            try {
                post.releaseConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

public static void main(String[] args) {
    String ipurl = iphoneurl+"您的手機号";
    String result = HttpTool.doHttpgetAPi(url);
    System.out.println(result );
}           

}

通過執行傳回的結果是:

{"response":{"13854510856":{"detail":{"area":[{"city":"煙台"}],"province":"山東","type":"domestic","operator":"移動"},"location":"山東煙台移動"}},"responseHeader":{"status":200,"time":1579079131507,"version":"1.1.0"}}