天天看点

使用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"}}