From c6c5a8ce0f42d46ea2fe0312c9eda67a49fd3917 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期一, 01 二月 2021 18:58:21 +0800 Subject: [PATCH] 完善根据IP屏蔽广告,创建会员实体 --- src/main/java/com/yeshi/buwan/util/IPUtil.java | 86 +++++++++++++++++++++++++----------------- 1 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/yeshi/buwan/util/IPUtil.java b/src/main/java/com/yeshi/buwan/util/IPUtil.java index cd8c277..2bab44e 100644 --- a/src/main/java/com/yeshi/buwan/util/IPUtil.java +++ b/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; + } } } -- Gitblit v1.8.0