Java 获取手机号码归属地以及运营商

maven配置

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>

Java代码

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;

import com.chinaedu.icec.blcu.common.model.phone.CheckMobilePlaceInfo;
import com.chinaeducloud.base.Context;
import com.chinaeducloud.configuration.AppConf;
/**
 * 
 * @Description:手机号码归属地 
 * @author: wangtianming
 * @date:   2021年6月30日
 */
public class CheckMobilePlaceLogic {

	 public static void main(String[] args) {
    	String iurl = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=187******04";
	    String result;
		try {
			result = CheckMobilePlaceLogic.doHttpget(iurl);
			String jsonString = result.split("=")[1].trim();
		    ObjectMapper objectMapper = new ObjectMapper();

			objectMapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);//设置可用单引号

			objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);//设置字段可以不用双引号包括

			JsonNode root = objectMapper.readTree(jsonString);
			System.out.println("归属地="+ root.path("province").asText());
			System.out.println("运营商="+ root.path("catName").asText());
		} catch (Exception e) {
			e.toString();
		}
	}
	
	/**
	 * http请求
	 * @return
	 * @throws Exception
	 */
	public static String doHttpget(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;
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值