调用淘宝ip库解析ip

来源:互联网 发布:java sun认证 怎么考 编辑:程序博客网 时间:2024/06/03 15:06

java实现调用淘宝ip库


1、淘宝ip库接口说明

  1. 请求接口(GET):
    /service/getIpInfo.php?ip=[ip地址字串]
  2. 响应信息:
    (json格式的)国家 、省(自治区或直辖市)、市(县)、运营商
  3. 返回数据格式:
    {“code”:0,”data”:{“ip”:”210.75.225.254”,”country”:”\u4e2d\u56fd”,”area”:”\u534e\u5317”,
    “region”:”\u5317\u4eac\u5e02”,”city”:”\u5317\u4eac\u5e02”,”county”:”“,”isp”:”\u7535\u4fe1”,
    “country_id”:”86”,”area_id”:”100000”,”region_id”:”110000”,”city_id”:”110000”,
    “county_id”:”-1”,”isp_id”:”100017”}}
    其中code的值的含义为,0:成功,1:失败。
  4. 访问限制
    每个用户的访问频率需小于10qps

2、java实现

import java.io.InputStream;import java.io.StringWriter;    import org.apache.commons.io.IOUtils;import org.apache.commons.lang.StringUtils;import org.apache.http.HttpEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.json.JSONObject;public class testIP {    public static void main(String[] args) {        String ipstr="59.57.218.113";        //调用淘宝ip库        DefaultHttpClient hc = new DefaultHttpClient();        InputStream is = null;        HttpGet http_get = new HttpGet(                "http://ip.taobao.com/service/getIpInfo.php?ip=" + ipstr);        IPAddr ip = new IPAddr();        int count = 0;        do {            count++;            try {                HttpEntity e = hc.execute(http_get).getEntity();                is = e.getContent();                StringWriter sw = new StringWriter();                IOUtils.copy(is, sw);                is.close();                String res = sw.toString();                System.out.println("-------------------返回的json--------------------------");                System.out.println(res);                JSONObject resp = new JSONObject(res);                if (resp.has("data")) {                    JSONObject data = resp.getJSONObject("data");                    if (data.has("ip")) {                        String rip = data.getString("ip");                        ip.setIp(rip);                    }                    if (data.has("country")) {                        ip.setCountry(data.getString("country"));                    }                    if (data.has("country_id")) {                        ip.setCountry_id(data.getString("country_id"));                    }                    if (data.has("area")) {                        ip.setArea(data.getString("area"));                    }                    if (data.has("area_id")) {                        ip.setArea_id(data.getString("area_id"));                    }                    if (data.has("region")) {                        ip.setRegion(data.getString("region"));                    }                    if (data.has("region_id")) {                        ip.setRegion_id(data.getString("region_id"));                    }                    if (data.has("city")) {                        String rcity = data.getString("city");                        ip.setCity(rcity);                    }                    if (data.has("city_id")) {                        ip.setCity_id(data.getString("city_id"));                    }                    if (data.has("isp_id")) {                        ip.setIsp_id(data.getString("isp_id"));                    }                    if (data.has("isp")) {                        ip.setIsp(data.getString("isp"));                    }                }            } catch (Exception e) {                e.printStackTrace();            }        } while (StringUtils.isBlank(ip.getCity()) && count < 10);//尝试10次        System.out.println("-------------------解析结果--------------------------");        System.out.println(ip.toString());    }}

结果:
——————-返回的json————————–
{“code”:0,”data”:{“country”:”\u4e2d\u56fd”,”country_id”:”CN”,”area”:”\u534e\u4e1c”,”area_id”:”300000”,”region”:”\u798f\u5efa\u7701”,”region_id”:”350000”,”city”:”\u53a6\u95e8\u5e02”,”city_id”:”350200”,”county”:”\u96c6\u7f8e\u533a”,”county_id”:”350211”,”isp”:”\u7535\u4fe1”,”isp_id”:”100017”,”ip”:”59.57.218.113”}}
——————-解析结果————————–
解析结果: [ip=59.57.218.113, country=中国, country_id=CN, area=华东, area_id=300000, region=福建省, region_id=350000, city=厦门市, city_id=350200, county=null, county_id=null, isp=电信, isp_id=100017]

附ip实体类代码:

public class IPAddr {    private String ip;    private String country;    private String country_id;    private String area;    private String area_id;    private String region;    private String region_id;    private String city;    private String city_id;    private String county;    private String county_id;    private String isp;    private String isp_id;    public String getIp() {        return ip;    }        public String getCountry() {        return country;    }        public void setCountry(String country) {        this.country = country;    }        public String getCountry_id() {        return country_id;    }        public void setCountry_id(String country_id) {        this.country_id = country_id;    }        public String getArea() {        return area;    }        public void setArea(String area) {        this.area = area;    }        public String getArea_id() {        return area_id;    }        public void setArea_id(String area_id) {        this.area_id = area_id;    }        public String getRegion() {        return region;    }        public void setRegion(String region) {        this.region = region;    }        public String getRegion_id() {        return region_id;    }        public void setRegion_id(String region_id) {        this.region_id = region_id;    }        public String getCity_id() {        return city_id;    }        public void setCity_id(String city_id) {        this.city_id = city_id;    }        public String getCounty() {        return county;    }        public void setCounty(String county) {        this.county = county;    }        public String getCounty_id() {        return county_id;    }        public void setCounty_id(String county_id) {        this.county_id = county_id;    }        public String getIsp_id() {        return isp_id;    }        public void setIsp_id(String isp_id) {        this.isp_id = isp_id;    }        public void setIp(String ip) {        this.ip = ip;    }        public String getCity() {        return city;    }        public void setCity(String city) {        this.city = city;    }        public String getIsp() {        return isp;    }        public void setIsp(String isp) {        this.isp = isp;    }    public String toString() {        return "解析结果: [ip=" + ip + ", country=" + country + ", country_id="                + country_id + ", area=" + area + ", area_id=" + area_id                + ", region=" + region + ", region_id=" + region_id + ", city="                + city + ", city_id=" + city_id + ", county=" + county                + ", county_id=" + county_id + ", isp=" + isp + ", isp_id="                + isp_id + "]";    }}
0 0
原创粉丝点击