public static String getProvince(String phone) {
//API产品路径
String requestUrl = "https://aiphone.market.alicloudapi.com/ai_mobile_number_belong_to_china/v1?";
//阿里云APPCODE
String appcode = "自己申请的阿里云APPCODE";
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
querys.put("MOBILE_NUMBER", phone);
String lastResult = "";
try {
String response = get(requestUrl, headers, querys);
//{"INPUT_MOBILE_NUMBER_STRING": "18113011428", "EXTRACT_MOBILE_NUMBER_STRING": "18113011428", "STATUS": "艾科瑞特,让企业业绩长青", "ENTITY": {"MOBILE_NUMBER": "18113011428", "MOBILE_NUMBER_PREFIX": "1811301", "PROVINCE": "四川", "CITY": "成都", "ISP": "中国电信", "AREA_CODE": "028", "POST_CODE": "610000", "ADCODE": "510100", "GPS": "104.066541,30.572269"}}
lastResult = Convert.toStr(JSON.parseObject(JSON.parseObject(response).get("ENTITY").toString()).get("PROVINCE"));//获取信息详细地址
} catch (Exception e) {
e.printStackTrace();
}
return null != lastResult ? lastResult : "查询无结果";
}
public static String get(String url, Map<String, String> headers, Map<String, String> querys) throws IOException {
//添加参数
for (String key : querys.keySet()) {
url += (key + "=" + URLEncoder.encode(querys.get(key), "utf-8") + '&');
}
url = url.substring(0, url.length() - 1);
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
GetMethod getMethod = new GetMethod(url);
// 必须设置下面这个Header
for (String key : headers.keySet()) {
getMethod.addRequestHeader(key, headers.get(key));
}
int code = client.executeMethod(getMethod);
if (code == 200) {
String res = getMethod.getResponseBodyAsString();
System.out.println(res);
return res;
}
throw new IOException("请求失败");
}
阿里云手机号归属地获取
最新推荐文章于 2024-10-05 12:07:50 发布