package com.zhengtian.tools.service.phone;
import java.io.IOException;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;
import com.zhengtian.tools.bean.phone.MobilePhone;
import com.zhengtian.tools.util.JsonUtils;
/**
* 手机信息查询工具类,主要实现根据手机号码查询手机归属地等信息。
*
* @author zhengtian
* @time 2012-12-26
*/
public class MobilePhoneTool {
/**
* 手机信息查询全局变量
*/
// 手机信息查询网址
private static final String URL = "http://api.showji.com/Locating/www.showji.com.aspx";
/**
* 上网代理全局变量
*/
// 代理Host
private static final String proxyHost = "10.10.10.78";
// 代理端口
private static final int proxyProt = 8080;
// 代理用户名
private static final String proxyUserName = "zhengtian";
// 代理密码
private static final String proxyPassword = "4rfv.com";
/**
* 响应包编码方式
*/
private static final String responseCharset = "UTF-8";
private MobilePhone getMobilePhone(long phone_number) {
HttpClient client = new HttpClient();
// 设置代理
//setProxy(client, proxyHost, proxyProt, proxyUserName, proxyPassword);
// get方式
HttpMethod method = new GetMethod(URL);
try {
// 设置get参数
NameValuePair m = new NameValuePair("m", String.valueOf(phone_number));
NameValuePair output = new NameValuePair("output", "json");
method.setQueryString(new NameValuePair[] { m, output });
// 服务器返回的状态(200:ok)
int statusCode = client.executeMethod(method);
if (statusCode == 200) {
// 服务器返回的信息
String responseBody = new String(method.getResponseBody(), responseCharset);
if (StringUtils.isNotEmpty(responseBody)) {
JSONObject json = JSONObject.fromObject(responseBody);
return convertJsonToMobilePhone(json);
}
System.out.println("查询结果为空");
} else {
System.out.println("查询失败");
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放连接
method.releaseConnection();
}
return null;
}
/**
* 将json对象转换为MobilePhone
*
* @param json
* @return
*/
private MobilePhone convertJsonToMobilePhone(JSONObject json) {
if (JsonUtils.isNotEmpty(json)) {
MobilePhone phone = new MobilePhone();
// 获取手机号码
long mobile = JsonUtils.getLongValue(json, "Mobile");
phone.setPhone_number(mobile);
// 获取省份
String province = JsonUtils.getStringValue(json, "Province");
phone.setProvince(province);
// 获取城市
String city = JsonUtils.getStringValue(json, "City");
phone.setCity(city);
// 获取城市区号
String areacode = JsonUtils.getStringValue(json, "AreaCode");
phone.setCity_code(areacode);
// 获取邮编
long postcode = JsonUtils.getLongValue(json, "PostCode");
phone.setZip_code(postcode);
// 获取公司
String corp = JsonUtils.getStringValue(json, "Corp");
phone.setCorporation_name(corp);
// 获取sim卡类型
String card = JsonUtils.getStringValue(json, "Card");
phone.setCard_type(card);
return phone;
}
return null;
}
/**
* 设置代理
*
* @param client
* @param proxyHost
* @param proxyProt
* @param userName
* @param password
*/
private void setProxy(HttpClient client, String proxyHost, int proxyProt, String userName, String password) {
// 设置HTTP代理IP和端口
client.getHostConfiguration().setProxy(proxyHost, proxyProt);
// 代理认证
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName, password);
client.getState().setProxyCredentials(AuthScope.ANY, creds);
}
public static void main(String[] args) {
long no = args != null && args.length > 0 ? Long.valueOf(args[0]) : 18658155313L;
if (args != null && args.length > 1) {
no = Long.valueOf(args[0]);
}
MobilePhoneTool phone = new MobilePhoneTool();
MobilePhone info = phone.getMobilePhone(no);
if (info != null) {
System.out.println(info.getPhoneInfo());
}
}
}