java通过免费接口获取ip地址的服务商信息

简介:
+关注继续查看

今天分享一个免费在线的小工具的开发代码就是通过淘宝提供的接口获取服务商信息,
工具地址: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"}}
}

}

相关文章
|
14天前
|
数据安全/隐私保护
基于SpringBoot+Vue+Mysql+Java 毕业生信息招聘平台系统(附源码)下
基于SpringBoot+Vue+Mysql+Java 毕业生信息招聘平台系统(附源码)
|
14天前
|
数据库
基于SpringBoot+Vue+Mysql+Java 毕业生信息招聘平台系统(附源码)中
基于SpringBoot+Vue+Mysql+Java 毕业生信息招聘平台系统(附源码)
|
14天前
|
安全 关系型数据库 MySQL
基于SpringBoot+Vue+Mysql+Java 毕业生信息招聘平台系统(附源码)上
基于SpringBoot+Vue+Mysql+Java 毕业生信息招聘平台系统(附源码)
|
22天前
|
存储 SQL Java
Java反射读取注解信息
Java反射读取注解信息
23 0
|
22天前
|
Java
Java获取线程基本信息的方法
Java获取线程基本信息的方法
14 0
|
1月前
|
Java Android开发
使用java实现qq邮箱信息的发送(纯java实现)
使用java实现qq邮箱信息的发送(纯java实现)
27 0
|
1月前
|
Java Spring
【Java 】 如何通过 反射 获取 注解信息 ?
在框架中如何通过 反射 获取到 类或方法 上的 注解信息
36 0
|
2月前
|
XML JSON 缓存
Java实现商品ID获取淘宝商品历史价格信息数据方法
Java实现商品ID获取淘宝商品历史价格信息数据方法
55 0
|
2月前
|
SQL 前端开发 JavaScript
基于java+springboot的旅游信息网站、旅游景区门票管理系统
该系统是基于java+springboot开发的旅游景区门票管理系统。是给师弟开发的大四实习作品。学习过程中,遇到问题可以咨询github作者。
41 0
|
3月前
|
Java
java上传图片获取文件信息 校验文件尺寸
一开始我是用的是下面这种方法,测试了几个图片都没有问题,然后随便去网上下载了个图片测试,竟然报了空指针,获取不到高度和宽度,于是我右键查看图片属性,详细信息竟然都是空的
相关产品
云迁移中心
推荐文章
更多