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;
}