早上一來,項目經理就說需要添加一個用戶ip歸屬地查詢功能,然后在網上搜羅半天,研究出一個比較簡單的方法,通過接口返回地址json數據
有百度接口,新浪接口,這里用的是淘寶ip接口
通過淘寶IP地址庫獲取IP位置
請求接口(GET):http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]
響應信息:(json格式的)國家 、省(自治區或直轄市)、市(縣)、運營商等
2018.2.26運行項目發現淘寶接口報錯502,於是找到一個替代的接口,新浪接口
String sip = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js";
直接將淘寶接口 換成這個就可以運行,過程一樣
2018/6/19 還是用淘寶ip接口
1 public static void main(String[] args) { 2 // 測試ip 221.232.245.73 湖北武漢 3 String ip = "221.232.245.73"; 4 String address = ""; 5 try { 6 address = addressUtils.getAddresses("ip="+ip, "utf-8"); 7 } catch (UnsupportedEncodingException e) { 8 // TODO Auto-generated catch block 9 e.printStackTrace(); 10 } 11 System.out.println(address); 12 // 輸出結果為:中國 湖北省 武漢市 13 } 14 15 16 17 /** 3 * @param content 18 * 請求的參數 格式為:name=xxx&pwd=xxx 19 * @param encoding 20 * 服務器端請求編碼。如GBK,UTF-8等 21 * @return 22 * @throws UnsupportedEncodingException 23 */ 24 public String getAddresses(String content, String encodingString) throws UnsupportedEncodingException { 25 // 這里調用pconline的接口 26 String urlStr = "http://ip.taobao.com/service/getIpInfo.php"; 27 // 從http://whois.pconline.com.cn取得IP所在的省市區信息 28 String returnStr = this.getResult(urlStr, content, encodingString); 29 if (returnStr != null) { 30 // 處理返回的省市區信息 31 System.out.println("IP====="+returnStr); 32 String[] temp = returnStr.split(","); 33 if(temp.length<3){ 34 return "0"; //無效IP,局域網測試 35 } 36 String region = (temp[5].split(":"))[1].replaceAll("\"", ""); 37 region = decodeUnicode(region); // 省 38 System.out.println("region = "+region); 39 40 String country = ""; 41 String area = ""; 42 // String region = ""; 43 String city = ""; 44 String county = ""; 45 String isp = ""; 46 System.out.println("temp的長度="+temp.length); 47 for (int i = 0; i < temp.length; i++) { 48 switch (i) { 49 如果使用的是新浪的接口,那這里的需要修改,case:3 4 5分別對應國家,省,市區 50 case 1: 51 country = (temp[i].split(":"))[2].replaceAll("\"", ""); 52 country = decodeUnicode(country); // 國家 53 break; 54 case 3: 55 area = (temp[i].split(":"))[1].replaceAll("\"", ""); 56 area = decodeUnicode(area); // 地區 57 break; 58 case 5: 59 region = (temp[i].split(":"))[1].replaceAll("\"", ""); 60 region = decodeUnicode(region); // 省份 61 break; 62 case 7: 63 city = (temp[i].split(":"))[1].replaceAll("\"", ""); 64 city = decodeUnicode(city); // 市區 65 break; 66 case 9: 67 county = (temp[i].split(":"))[1].replaceAll("\"", ""); 68 county = decodeUnicode(county); // 地區 69 break; 70 case 11: 71 isp = (temp[i].split(":"))[1].replaceAll("\"", ""); 72 isp = decodeUnicode(isp); // ISP公司 73 break; 74 } 75 } 76 System.out.println(country+"="+area+"="+region+"="+city+"="+county+"="+isp); 77 return region; 78 } 79 return null; 80 } 81 82 83 84 /** 85 * @param urlStr 86 * 請求的地址 87 * @param content 88 * 請求的參數 格式為:name=xxx&pwd=xxx 89 * @param encoding 90 * 服務器端請求編碼。如GBK,UTF-8等 91 * @return 92 */ 93 private String getResult(String urlStr, String content, String encoding) { 94 URL url = null; 95 HttpURLConnection connection = null; 96 try { 97 url = new URL(urlStr); 98 connection = (HttpURLConnection) url.openConnection(); // 新建連接實例 99 connection.setConnectTimeout(2000); // 設置連接超時時間,單位毫秒 100 connection.setReadTimeout(2000); // 設置讀取數據超時時間,單位毫秒 101 connection.setDoOutput(true); // 是否打開輸出流 true|false 102 connection.setDoInput(true); // 是否打開輸入流true|false 103 connection.setRequestMethod("POST"); // 提交方法POST|GET 104 connection.setUseCaches(false); // 是否緩存true|false 105 connection.connect(); // 打開連接端口 106 DataOutputStream out = new DataOutputStream(connection.getOutputStream());// 打開輸出流往對端服務器寫數據 107 out.writeBytes(content); // 寫數據,也就是提交你的表單 name=xxx&pwd=xxx 108 out.flush(); // 刷新 109 out.close(); // 關閉輸出流 110 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));// 往對端寫完數據對端服務器返回數據 ,以BufferedReader流來讀取 111 StringBuffer buffer = new StringBuffer(); 112 String line = ""; 113 while ((line = reader.readLine()) != null) { 114 buffer.append(line); 115 } 116 reader.close(); 117 return buffer.toString(); 118 } catch (IOException e) { 119 e.printStackTrace(); 120 } finally { 121 if (connection != null) { 122 connection.disconnect(); // 關閉連接 123 } 124 } 125 return null; 126 } 127 128 /** 129 * unicode 轉換成 中文 130 * 131 * @author fanhui 2007-3-15 132 * @param theString 133 * @return 134 */ 135 public static String decodeUnicode(String theString) { 136 char aChar; 137 int len = theString.length(); 138 StringBuffer outBuffer = new StringBuffer(len); 139 for (int x = 0; x < len;) { 140 aChar = theString.charAt(x++); 141 if (aChar == '\\') { 142 aChar = theString.charAt(x++); 143 if (aChar == 'u') { 144 int value = 0; 145 for (int i = 0; i < 4; i++) { 146 aChar = theString.charAt(x++); 147 switch (aChar) { 148 case '0': 149 case '1': 150 case '2': 151 case '3': 152 case '4': 153 case '5': 154 case '6': 155 case '7': 156 case '8': 157 case '9': 158 value = (value << 4) + aChar - '0'; 159 break; 160 case 'a': 161 case 'b': 162 case 'c': 163 case 'd': 164 case 'e': 165 case 'f': 166 value = (value << 4) + 10 + aChar - 'a'; 167 break; 168 case 'A': 169 case 'B': 170 case 'C': 171 case 'D': 172 case 'E': 173 case 'F': 174 value = (value << 4) + 10 + aChar - 'A'; 175 break; 176 default: 177 throw new IllegalArgumentException( 178 "Malformed encoding."); 179 } 180 } 181 outBuffer.append((char) value); 182 } else { 183 if (aChar == 't') { 184 aChar = '\t'; 185 } else if (aChar == 'r') { 186 aChar = '\r'; 187 } else if (aChar == 'n') { 188 aChar = '\n'; 189 } else if (aChar == 'f') { 190 aChar = '\f'; 191 } 192 outBuffer.append(aChar); 193 } 194 } else { 195 outBuffer.append(aChar); 196 } 197 } 198 return outBuffer.toString(); 199 }
輸出結果為
IP====={"code":0,
"data":{
"ip":"221.232.245.73", //ip
"country":"中國", //國家
"area":"", //區
"region":"湖北", //省
"city":"武漢", //市
"county":"XX",
"isp":"電信", //寬帶運營
"country_id":"CN", //國家id
"area_id":"", //區編碼
"region_id":"420000", //省編碼
"city_id":"420100", //市編碼
"county_id":"xx",
"isp_id":"100017"}} region = 武漢 temp的長度=14 中國==武漢=電信==420100