java通过免费接口获取ip地址的服务商信息
今天分享一个免费在线的小工具的开发代码就是通过淘宝提供的接口获取服务商信息,
工具地址:http://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"}}
}
}
java通过免费接口获取ip地址的服务商信息的更多相关文章
- JAVA从本机获取IP地址
JAVA从本机获取IP地址 论述: 此篇博客是在工作的时候,需要获得当前网络下面正确的ip地址,在网上查阅很多博客,网上一个比较普遍的说法是通过InetAddress.getLocalHost().g ...
- 获取ip地址及城市信息
大家好,今天给大家分享的是一个简单的知识获取登录用户的ip地址及城市信息,lz是一个小白,如果有哪些错误的地方 欢迎大家指出 东西很简单,直接上代码 [HttpPost] public string ...
- thinkphp获取ip地址及位置信息
ThinkPHP\Common\funcitons.php下有一个get_client_ip()能够获取ip地址 但是有时候不够准确 找到了下面一段来 function get_client_ip($ ...
- [转] JAVA从本机获取IP地址
[From] https://www.cnblogs.com/xiaoBlog2016/p/7076230.html 论述: 此篇博客是在工作的时候,需要获得当前网络下面正确的ip地址,在网上查阅很多 ...
- java通过请求对象获取ip地址、获取ip地址
/** * 获取登录ip */ public String getIp(){ HttpServletRequest request = this.getRequest(); String ip = & ...
- 前端借助接口获取ip地址
<script language="javascript" src="http://www.codefans.net/ajaxjs/jquery1.3.2.js&q ...
- 正确JAVA从本机获取IP地址的方法
https://www.cnblogs.com/xiaoBlog2016/p/7076230.html
- PHP 获取IP地址位置信息「聚合数据API」
聚合数据 提供了[查询IP所属区域]的服务接口,只需要以 GET 请求的方式向 API 传入 IP地址 和 APPKEY 即可获得查询结果. 这里的难点主要在于如何通过PHP获取客户端IP地址,以及如 ...
- js获取IP地址方法总结_转
js代码获取IP地址的方法,如何在js中取得客户端的IP地址.原文地址:js获取IP地址的三种方法 http://www.jbxue.com/article/11338.html 1,js取得IP地址 ...
随机推荐
- 吴裕雄--天生自然C++语言学习笔记:C++ 修饰符类型
C++ 允许在 char.int 和 double 数据类型前放置修饰符.修饰符用于改变基本类型的含义,所以它更能满足各种情境的需求. 下面列出了数据类型修饰符: signed unsigned lo ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:输出
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- k8s yaml 文件中字段类型:
1.<Object> 对象类型 metadata: name: namespace: 2.<[]Object> 对象列表类型 containers: - name: ...
- JS实现时间选择器
源代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- H3C S10512虚拟化配置
软件版本:Version 7.1.070, Release 7585P05 1.配置SW1#设置SW1的成员编号为1,创建IRF端口2,并将它与物理接口Ten-G0/0/45.Ten-G0/0/46. ...
- Django模型基础(三)——关系表的数据操作
模型之间可以有三种表关系,即一对一,一对多和多对多.表关联之间的数据操作在Django中可以很方便的操作到.在模型中,表关联的字段类型是关联表的实例,而不是字段本身类型.关联字段在数据库中会在其后补上 ...
- 统计_statistics_不同的人_大样本_分析_统计方法_useful ?
统计_statistics_不同的人_大样本_分析_
- 条款02:尽量以const,enum,inline替换#define
目录 1. 总结 2. 使用const常量或enum替换宏常量 class外部的常量指针 class专属常量 1. 总结 对于单纯常量,最好以const常量或enum替换#define 对于宏代码段, ...
- JunOS SRX firewal Web authentication(转)
转载自:https://srxasa.wordpress.com/2011/12/11/junos-srx-firewal-web-authentication/ JunOS SRX firewal ...
- 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制
spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...