利用httpclient测试手机号码归属地接口(todo)

Posted xinxin1994

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用httpclient测试手机号码归属地接口(todo)相关的知识,希望对你有一定的参考价值。

indexof()用法

package com.netease;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;

public class HttpClientJemter {

    public static CloseableHttpClient httpClient = null;

    @BeforeClass
    public static  void setUp(){
        httpClient = HttpClients.createDefault();
    }
    @Test
    public void phoneTest() throws ClientProtocolException , IOException {
        String phone = "13521275380";
        HttpGet httpGet = new HttpGet("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+phone);
        httpGet.addHeader("EagleEye-TraceId","0b5205fc15872856373491041e0635");
        CloseableHttpResponse res = httpClient.execute(httpGet);
        try{
            int statusCode = res.getStatusLine().getStatusCode();
            System.out.println(statusCode);
            String resBody = EntityUtils.toString(res.getEntity());
            System.out.println(resBody);
            int t1 = resBody.indexOf("province")+9;
            int t2 = resBody.indexOf("‘",t1);
            String city = resBody.substring(t1,t2);  //根据两个双引号截取city的内容
            city = StringEscapeUtils.unescapeJava(city);//将转换成中文,即可获得归属地
            System.out.println(city);

        }finally {
            res.close();
        }
    }
}

 

以上是关于利用httpclient测试手机号码归属地接口(todo)的主要内容,如果未能解决你的问题,请参考以下文章

java 手机号码归属地查询

免费的API-手机号码归属地接口

调用API接口,查询手机号码归属地

免费手机号码归属地API查询接口

免费手机号码归属地API查询接口

php 手机号归属地显示查询