今天分享一個免費線上的小工具的開發代碼就是通過淘寶提供的接口擷取服務商資訊,
工具位址:www.yzcopen.com/seo/ipadress
代碼如下:
public class YzcPattern {
/**
* ip位址接口
*/
private final static String ipurl ="http://ip.taobao.com/service/getIpInfo.php?ip=";
/**
* 判斷ip
* @param text
* @return
*/
public static boolean ipCheck(String text) {
if (text != null && !text.isEmpty()) {
// 定義正規表達式
String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
// 判斷ip位址是否與正規表達式比對
if (text.matches(regex)) {
return true;
// 傳回判斷資訊
//return text + "\n是一個合法的IP位址!";
} else {
return false;
// 傳回判斷資訊
//return text + "\n不是一個合法的IP位址!";
}
}
return false;
}
/**
* 讀取IP的
* @param getAccessTokenUrl
* @return
*/
public static String getAuth(String getAccessTokenUrl) {
HttpURLConnection connection = null;
try {
URL realUrl = new URL(getAccessTokenUrl);
// 打開和URL之間的連接配接
connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestProperty("User-Agent", Const.UserAgent);
connection.setRequestMethod("GET");
connection.setRequestProperty("Charsert", "UTF-8"); //設定請求編碼
connection.setRequestProperty("Content-Type",
"application/json");
connection.connect();
// 定義 BufferedReader輸入流來讀取URL的響應
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String result = "";
String line;
while ((line = in.readLine()) != null) {
result += line;
}
/**
* 傳回結果示例
*/
return result;
} catch (Exception e) {
e.printStackTrace();
}finally{
if(connection!=null){
connection.disconnect();
}
}
return null;
}
public static void main(String[] args) throws Exception {
String ip="您的ip";
boolean bo = YzcPattern.ipCheck(ip);
if(bo){
String url = ipurl+ip;
String result = getAuth(url);
}
//獲得的結果 {"code":0,"data":{"ip":"58.87.124.194","country":"中國","area":"","region":"天津","city":"天津","county":"XX","isp":"電信","country_id":"CN","area_id":"","region_id":"120000","city_id":"120100","county_id":"xx","isp_id":"100017"}}
}
}