【Java 根据网址获取IP以及地理位置】

根据IP获取地理位置

    public static String getAddress(String ip) {
        try {
            URL realUrl = new URL("http://whois.pconline.com.cn/ipJson.jsp?ip=" + ip + "&json=true");
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            conn.setRequestMethod("GET");
            conn.setUseCaches(false);
            conn.setReadTimeout(6000);
            conn.setConnectTimeout(6000);
            conn.setInstanceFollowRedirects(false);
            int code = conn.getResponseCode();
            StringBuilder sb = new StringBuilder();
            String ipaddr = "";
            if (code == 200) {
                InputStream in = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in, "GBK"));//指定编码格式
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                System.out.println("sb:" + sb);
                JSONObject jsonObject = JSON.parseObject(sb.toString());

                System.out.println("json方式:" + jsonObject);

                String pro = sb.substring((sb.indexOf("pro") + 6), sb.indexOf("proCode") - 3);
                String city = sb.substring((sb.indexOf("city") + 7), sb.indexOf("cityCode") - 3);
                String addr = (sb.substring(sb.indexOf("addr") + 7, sb.indexOf("regionNames") - 3)).trim();
                String ipAddr = pro + city;
                if (StringUtils.isBlank(ipAddr)) {
                    ipaddr = addr;
                } else {
                    ipaddr = pro.equals(city) ? pro : pro + city;
                }

            }
            return ipaddr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


    public static void main(String[] args) {
        String s = getAddress("30.233.96.241");
        System.out.println(s);
    }

输出

在这里插入图片描述

详解

http://whois.pconline.com.cn/ipJson.jsp?ip=xxx
这个接口是一个简单的IP查询接口,用于查询指定IP地址的地理位置信息。它可以通过GET请求,将需要查询的IP地址作为参数传递给接口,然后接口会返回包含该IP地址地理位置信息的JSON格式数据。

具体来说,该接口的URL是"http://whois.pconline.com.cn/ipJson.jsp?ip=",在URL的末尾需要拼接上要查询的IP地址。例如,如果要查询IP地址为"123.123.123.123"的地理位置信息,可以使用以下URL进行请求:

“http://whois.pconline.com.cn/ipJson.jsp?ip=123.123.123.123”

接口的返回结果中包含了IP地址的大概位置、所属地区、运营商等信息。请注意,这个接口可能是一个第三方服务,使用时需要遵守接口的使用规则和限制。

根据网址获取地理位置

代码实现和上述基本一致,不同的是根据网址获取IP,查询IP地理位置也更换为百度提供的接口

    public static void main(String[] args) {
        String hostname = "www.baidu.com"; // 替换为你要获取IP地址的网址

        try {
            InetAddress inetAddress = InetAddress.getByName(hostname);
            String ipAddress = inetAddress.getHostAddress();
            System.out.println("网址的IP地址为:" + ipAddress);

            URL realUrl = new URL("https://qifu-api.baidubce.com/ip/geo/v1/district?ip=" + ipAddress);
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            conn.setRequestMethod("GET");
            conn.setUseCaches(false);
            conn.setReadTimeout(6000);
            conn.setConnectTimeout(6000);
            conn.setInstanceFollowRedirects(false);
            int code = conn.getResponseCode();
            StringBuilder sb = new StringBuilder();
            if (code == 200) {
                InputStream in = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));//指定编码格式
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                System.out.println("sb:" + sb);
                JSONObject jsonObject = JSON.parseObject(sb.toString());
                System.out.println(jsonObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

输出

在这里插入图片描述

详解

https://qifu-api.baidubce.com/ip/geo/v1/district?ip=xxx
这个接口是百度云智能的IP地理位置查询接口。它可以通过GET请求,将需要查询的IP地址作为参数传递给接口,然后接口会返回包含该IP地址地理位置信息的JSON格式数据。

具体来说,该接口的URL是"https://qifu-api.baidubce.com/ip/geo/v1/district?ip=",在URL的末尾需要拼接上要查询的IP地址。例如,如果要查询IP地址为"123.123.123.123"的地理位置信息,可以使用以下URL进行请求:

“https://qifu-api.baidubce.com/ip/geo/v1/district?ip=123.123.123.123”

接口的返回结果中包含了IP地址所在的国家、省份、城市、区县等详细信息。这个接口是百度云提供的服务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值