admin
2021-02-06 cad915058c3c53bf328a8ae9ca9bc7de099caba7
src/main/java/com/yeshi/buwan/util/IPUtil.java
@@ -261,42 +261,17 @@
        return ip;
    }
    public static Map<String, String> getIPInfo(String ip) {
        Map<String, String> map = new HashMap<String, String>();
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
        try {
            client.executeMethod(method);
            String result = method.getResponseBodyAsString();
            JSONObject object = JSONObject.fromObject(result);
            if (object.optInt("code") == 0) {
                map.put("city", object.optJSONObject("data").optString("city"));
                map.put("country", object.optJSONObject("data").optString("country"));
                return map;
            }
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }
    public static String getIPContry(String ip) {
        String res = new ShowApiRequest("http://route.showapi.com/2152-1", "49053", "983e020ce60042cd95f39b3ebd4b9563")
                .addTextPara("ip", ip)
                .post();
        JSONObject json = JSONObject.fromObject(res);
        if (json.optInt("showapi_res_code") == 0) {
            JSONObject body = json.optJSONObject("showapi_res_body");
            if (body != null)
                return body.optString("country");
        }
        return "";
        IPInfo ipInfo = getIPInfo(ip);
        return ipInfo == null ? "" : ipInfo.getCountry();
    }
    public static String getIPProvince(String ip) {
        IPInfo ipInfo = getIPInfo(ip);
        return ipInfo == null ? "" : ipInfo.getProvince();
    }
    public static IPInfo getIPInfo(String ip) {
        try {
            String res = new ShowApiRequest("http://route.showapi.com/2152-1", "49053", "983e020ce60042cd95f39b3ebd4b9563")
                    .addTextPara("ip", ip)
@@ -304,13 +279,54 @@
            JSONObject json = JSONObject.fromObject(res);
            if (json.optInt("showapi_res_code") == 0) {
                JSONObject body = json.optJSONObject("showapi_res_body");
                if (body != null)
                    return body.optString("state");
                if (body != null) {
                    String country = body.optString("country");
                    String state = body.optString("state");
                    String city = body.optString("city");
                    return new IPInfo(country, state, city);
                }
            }
        } catch (Exception e) {
        }
        return "";
        return null;
    }
    public static class IPInfo {
        private String province;
        private String city;
        private String country;
        public IPInfo(String country, String province, String city) {
            this.province = province;
            this.city = city;
            this.country = country;
        }
        public String getProvince() {
            return province;
        }
        public void setProvince(String province) {
            this.province = province;
        }
        public String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public String getCountry() {
            return country;
        }
        public void setCountry(String country) {
            this.country = country;
        }
    }
}