在系統中,網站的頭部一般都有顯示是哪個城市的,使用者進入到網站的首頁後,預設城市應該是使用者本地的城市資訊,例如:北京,網站就要根據你的IP位址的資訊,查詢資料,擷取北京部分的資料,呵呵,當然我可能描述的不是很清楚,但是可以了解成,通過IP位址定位地理資訊就行。很多人現在使用以QQ資料庫為基礎擷取位址資訊,但不完整、而且不規範。網際網路提供很多其他接口可以完成這項功能.
接口如下:
通過淘寶IP位址庫擷取IP位置
1. 請求接口(GET):http://ip.taobao.com/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:失敗。
新浪的接口 :http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.192.3.42
傳回值
var remote_ip_info = {“ret”:1,”start”:”218.192.0.0”,”end”:”218.192.7.255”,”country”:”\u4e2d\u56fd”,”province”:”\u5e7f\u4e1c”,”city”:”\u5e7f\u5dde”,”district”:”“,”isp”:”\u6559\u80b2\u7f51”,”type”:”\u5b66\u6821”,”desc”:”\u5e7f\u5dde\u5927\u5b66\u7eba\u7ec7\u670d\u88c5\u5b66\u9662”};
通過jq
ry 擷取相應的資料
$.getScript(‘資料接口’,function(){
//新浪:remote_ip_info.country
})
根據騰訊IP分享計劃的位址擷取IP所在地
http://ip.qq.com/cgi-bin/searchip?searchip1=218.192.3.42
package com.gocheck.common.utils.ipmanager;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 根據IP位址擷取詳細的地域資訊
* @project:personGocheck
* @class:AddressUtils.java
* @author:heguanhua E-mail:[email protected]
* @date:Nov 14, 2012 6:38:25 PM
*/
public class AddressUtils {
/**
*
* @param content
* 請求的參數 格式為:name=xxx&pwd=xxx
* @param encoding
* 伺服器端請求編碼。如GBK,UTF-8等
* @return
* @throws UnsupportedEncodingException
*/
public String getAddresses(String content, String encodingString)
throws UnsupportedEncodingException {
// 這裡調用pconline的接口
String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
// 從http://whois.pconline.com.cn取得IP所在的省市區資訊
String returnStr = this.getResult(urlStr, content, encodingString);
if (returnStr != null) {
// 處理傳回的省市區資訊
System.out.println(returnStr);
String[] temp = returnStr.split(",");
if(temp.length<){
return "0";//無效IP,區域網路測試
}
String region = (temp[].split(":"))[].replaceAll("\"", "");
region = decodeUnicode(region);// 省份
/**
* String country = ""; String area = ""; String region = ""; String
* city = ""; String county = ""; String isp = ""; for(int i=0;i<temp.length;i++){
* switch(i){ case 1:country =
* (temp[i].split(":"))[2].replaceAll("\"", ""); country =
* decodeUnicode(country);//國家 break; case 3:area =
* (temp[i].split(":"))[1].replaceAll("\"", ""); area =
* decodeUnicode(area);//地區 break; case 5:region =
* (temp[i].split(":"))[1].replaceAll("\"", ""); region =
* decodeUnicode(region);//省份 break; case 7:city =
* (temp[i].split(":"))[1].replaceAll("\"", ""); city =
* decodeUnicode(city);//市區 break; case 9:county =
* (temp[i].split(":"))[1].replaceAll("\"", ""); county =
* decodeUnicode(county);//地區 break; case 11:isp =
* (temp[i].split(":"))[1].replaceAll("\"", ""); isp =
* decodeUnicode(isp);//ISP公司 break; } }
*/
// System.out.println(country+"="+area+"="+region+"="+city+"="+county+"="+isp);
return region;
}
return null;
}
/**
* @param urlStr
* 請求的位址
* @param content
* 請求的參數 格式為:name=xxx&pwd=xxx
* @param encoding
* 伺服器端請求編碼。如GBK,UTF-8等
* @return
*/
private String getResult(String urlStr, String content, String encoding) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();// 建立連接配接執行個體
connection.setConnectTimeout();// 設定連接配接逾時時間,機關毫秒
connection.setReadTimeout();// 設定讀取資料逾時時間,機關毫秒
connection.setDoOutput(true);// 是否打開輸出流 true|false
connection.setDoInput(true);// 是否打開輸入流true|false
connection.setRequestMethod("POST");// 送出方法POST|GET
connection.setUseCaches(false);// 是否緩存true|false
connection.connect();// 打開連接配接端口
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());// 打開輸出流往對端伺服器寫資料
out.writeBytes(content);// 寫資料,也就是送出你的表單 name=xxx&pwd=xxx
out.flush();// 重新整理
out.close();// 關閉輸出流
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), encoding));// 往對端寫完資料對端伺服器傳回資料
// ,以BufferedReader流來讀取
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();// 關閉連接配接
}
}
return null;
}
/**
* unicode 轉換成 中文
*
* @author fanhui 2007-3-15
* @param theString
* @return
*/
public static String decodeUnicode(String theString) {
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = ; x < len;) {
aChar = theString.charAt(x++);
if (aChar == '\\') {
aChar = theString.charAt(x++);
if (aChar == 'u') {
int value = ;
for (int i = ; i < ; i++) {
aChar = theString.charAt(x++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << ) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << ) + + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << ) + + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed encoding.");
}
}
outBuffer.append((char) value);
} else {
if (aChar == 't') {
aChar = '\t';
} else if (aChar == 'r') {
aChar = '\r';
} else if (aChar == 'n') {
aChar = '\n';
} else if (aChar == 'f') {
aChar = '\f';
}
outBuffer.append(aChar);
}
} else {
outBuffer.append(aChar);
}
}
return outBuffer.toString();
}
// 測試
public static void main(String[] args) {
AddressUtils addressUtils = new AddressUtils();
// 測試ip 219.136.134.157 中國=華南=廣東省=廣州市=越秀區=電信
String ip = "122.49.20.247";
String address = "";
try {
address = addressUtils.getAddresses("ip="+ip, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(address);
// 輸出結果為:廣東省,廣州市,越秀區
}
}